Home > Backend Development > Golang > An article to help you understand the basics of Go language pointers

An article to help you understand the basics of Go language pointers

Release: 2023-07-24 17:27:01
forward
1060 people have browsed it

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),说明不是一个地址,就是一个值
Copy after login

The memory distribution diagram is as follows.

An article to help you understand the basics of Go language pointers

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
Copy after login

如图所示。

An article to help you understand the basics of Go language pointers

如果这两个能理解,恭喜你,指针已经会了一半了。


引用类型指针的理解

先看这样的代码。

var studentList = []string{"张三", "李四"}//一个切片
fmt.Println(studentList)        //结果:[张三 李四]
fmt.Printf("%p\n", studentList) //结果:0xc0000044a0
//去地址
fmt.Printf("%p\n", &studentList) //结果:0xc0000044a0
Copy after login

内存分布图如下。

An article to help you understand the basics of Go language pointers


值类型和引用类型

值类型

在Go中,值类型主要有。

int, float, bool , string, array, struct(structure)

The memory distribution is roughly as follows.

An article to help you understand the basics of Go language pointers

## Note: is like String , array, structureThese 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.

SlicemapPipeline (chan)

The memory distribution is roughly as follows.

An article to help you understand the basics of Go language pointers


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 GCRecycling, 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类型的
Copy after login

*int是一个整体,表示c这个变量只能接收int类型地址

代码

package main


import "fmt"


func main() {
    var c *int
    var d int = 1
    //c = d//错误需要的是d的地址
    c = &d
    fmt.Println(c)
}
Copy after login

执行结果。

An article to help you understand the basics of Go language pointers

可以看到打印的也是一个地址,但是内存图还是基本类型图。

An article to help you understand the basics of Go language pointers

如果要打印c的值,直接*c就好了,取得就是地址里面对应得值了。

fmt.Println(*c)
Copy after login

关于函数

我们一直在强调,操作只会操作上面的,函数同理。

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是引用类型,所以传进去的时候就是地址,栈上面的就是地址
}
Copy after login

执行结果。

An article to help you understand the basics of Go language pointers


总结

上述我们主要讲述了基本类型指针引用类型指针,也叫做值类型引用类型,并且画出了值类型引用类型内存的本质区分图,后来又讲了&*的区别,还有函数传参的本质是什么,希望对大家的学习有帮助。

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!

Related labels:
source:Go语言进阶学习
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template