


An article to help you understand the basics of Go language pointers
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

When using sql.Open, why doesn’t the DSN report an error? In Go language, sql.Open...
