Introduction
Because the Go language is a C-like language, in order to improve efficiency, it is still retained pointer.
But if you have never been exposed to pointers, you may need to think more about learning pointers.
The pointer is usually also called the novice's magic trick.
##Understanding of basic type pointers
First look at these two lines of code .
var n1 int = 666 fmt.Println(n1)//结果:666 fmt.Printf("%p\n",n1)//结果:%!p(int=666),说明不是一个地址,就是一个值
The memory distribution diagram is as follows.
Look at these two lines of code again, &## is used here
#. var n1 int = 1
//表示取n1的地址
fmt.Println(&n1)//结果:0xc00000a0b8
fmt.Printf("%p\n",&n1)//结果:0xc00000a0b8
如图所示。
如果这两个能理解,恭喜你,指针已经会了一半了。
引用类型指针的理解
先看这样的代码。
var studentList = []string{"张三", "李四"}//一个切片 fmt.Println(studentList) //结果:[张三 李四] fmt.Printf("%p\n", studentList) //结果:0xc0000044a0 //去地址 fmt.Printf("%p\n", &studentList) //结果:0xc0000044a0
内存分布图如下。
值类型和引用类型
值类型
在Go中,值类型主要有。
int
, float
, bool
, string
, array
, struct(structure)
The memory distribution is roughly as follows.
## Note: is like String
, array, structure
These belong to Continuous storage , variables point to is their The first address, the rest will be calculated based on the length.
Reference types
In Go, the main reference types are.
Slice
,map
,Pipeline (chan)
The memory distribution is roughly as follows.
The difference between stack memory and heap memory
The stack memory is in storage and can only store some simple things, such as numbers, characters, floating point numbers and the like , but the programmer does not need to reclaim the memory allocated by the stack memory, it is reclaimed by the system itself. And the performance is very high.
The heap memory is relatively rich in storage. You can store it as you like, like map, or stuff it into whatever you want. However, the memory allocated by the heap memory needs to be recycled by the programmer. Typical example, C
, if the language consists of GC
by GC
Recycling, the performance is a little bit weaker..., but people can save it at will, how casual.
&
and The meaning of *
&
is called the address character.
*
is called the receiving address character.
Example
var c *int//*int是一个整体,说明c这个变量只能接收int类型的
*int
是一个整体,表示c这个变量只能接收int
类型的地址。
代码
package main import "fmt" func main() { var c *int var d int = 1 //c = d//错误需要的是d的地址 c = &d fmt.Println(c) }
执行结果。
可以看到打印的也是一个地址,但是内存图还是基本类型图。
如果要打印c
的值,直接*c
就好了,取得就是地址里面对应得值了。
fmt.Println(*c)
关于函数
我们一直在强调,操作只会操作栈上面的值,函数同理。
package main import "fmt" func say1(x int) { //x int 相当于隐藏了一行代码 //隐藏的代码时 var x int = x,一定要记住这个 fmt.Printf("say1:%p\n", x) } func say2(x *int) { //隐藏的代码是 var x *int = x,x是一个地址 fmt.Printf("say2:%p\n", x) } func say3(x []int) { //隐藏的代码是 var x []int = x,因为x是引用类型,所以x是一个地址 fmt.Printf("say3:%p\n", x) } func main() { say1(1)//栈上面是1,所以传进去就是1 var x1 = 1 say2(&x1)//say只能接收整数地址 var x2 = []int{1, 1} say3(x2)//x2是引用类型,所以传进去的时候就是地址,栈上面的就是地址 }
执行结果。
总结
上述我们主要讲述了基本类型指针和引用类型指针,也叫做值类型和引用类型,并且画出了值类型和引用类型内存的本质区分图,后来又讲了&
和*
的区别,还有函数传参的本质是什么,希望对大家的学习有帮助。
The above is the detailed content of An article to help you understand the basics of Go language pointers. For more information, please follow other related articles on the PHP Chinese website!

go语言有缩进。在go语言中,缩进直接使用gofmt工具格式化即可(gofmt使用tab进行缩进);gofmt工具会以标准样式的缩进和垂直对齐方式对源代码进行格式化,甚至必要情况下注释也会重新格式化。

go语言叫go的原因:想表达这门语言的运行速度、开发速度、学习速度(develop)都像gopher一样快。gopher是一种生活在加拿大的小动物,go的吉祥物就是这个小动物,它的中文名叫做囊地鼠,它们最大的特点就是挖洞速度特别快,当然可能不止是挖洞啦。

是,TiDB采用go语言编写。TiDB是一个分布式NewSQL数据库;它支持水平弹性扩展、ACID事务、标准SQL、MySQL语法和MySQL协议,具有数据强一致的高可用特性。TiDB架构中的PD储存了集群的元信息,如key在哪个TiKV节点;PD还负责集群的负载均衡以及数据分片等。PD通过内嵌etcd来支持数据分布和容错;PD采用go语言编写。

go语言需要编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言,也就说Go语言程序在运行之前需要通过编译器生成二进制机器码(二进制的可执行文件),随后二进制文件才能在目标机器上运行。

go语言能编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言。对Go语言程序进行编译的命令有两种:1、“go build”命令,可以将Go语言程序代码编译成二进制的可执行文件,但该二进制文件需要手动运行;2、“go run”命令,会在编译后直接运行Go语言程序,编译过程中会产生一个临时文件,但不会生成可执行文件。

删除map元素的两种方法:1、使用delete()函数从map中删除指定键值对,语法“delete(map, 键名)”;2、重新创建一个新的map对象,可以清空map中的所有元素,语法“var mapname map[keytype]valuetype”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use