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.
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.
In Go, the main reference types are.
Slice
,map
,Pipeline (chan)
The memory distribution is roughly as follows.
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!