This article is provided by the go language tutorial column to introduce to you under what circumstances use Go pointer (Go Pointer). I hope it will be helpful to friends in need!
The use of pointers in Go code is not very friendly to novices, especially when it is difficult to distinguish usage scenarios.
I think one of the biggest misunderstandings when using pointers is that pointers in Go are very similar to pointers in C language. However, that is not the case. Pointers don't work in Go the same way they do in C/C.
This article will discuss how to use Go pointers correctly.
Wrong conclusion: Using pointers is better?
It is generally believed that applications will run faster when using pointers because they avoid copying values all the time. It’s no surprise that in Go we have the same idea.
However, pointer passing in Go is generally slower than value passing. This is a consequence of Go being a garbage-collected language. When you pass a pointer to a function, Go needs to perform escape analysis to determine whether the variable should be stored on the heap or the stack. This already adds some extra overhead, but otherwise the variables can be stored in the heap. When you store a variable in the heap, you also lose time while the GC is executing.
A convenient feature of Go is that you can check what escape analysis has done by executing the command go build -gcflags="-m"
. If you do this, Go will tell you whether a variable escapes to the heap:
./main.go:44:20: greet ... argument does not escape ./main.go:44:21: greeting escapes to heap ./main.go:44:21: name escapes to heap
If a variable does not escape to the heap, it stays on the stack. The stack does not need a garbage collector to clear variables, it only does push/pop
operations.
If any content is passed by value, it will always be processed on the stack, which will not cause garbage collection overhead. (The GC will run by default. Less content in the heap means the GC has less to do).
Now you know, using pointers will reduce performance, so when needuse pointers?
Copying large data structures
Do pointers always perform worse than value transfer? That's obviously not the case. When dealing with large data structures, pointers come into play. This may cause the cost of garbage collection to be offset by the cost of copying large amounts of data.
When I mention this, I’m always asked ‘how big should that big data be’?
I think there is no fixed value here. Anything related to performance should be benchmarked. Go has built-in powerful benchmarking tools, which can be fully utilized
Variability
The only way to modify function parameters is to pass pointers. By default, modifications to values are performed on the copy. Therefore these modifications cannot be reflected in the function that calls it.
Look at the following code:
type person struct { name string }func main() { p := person{"Richard"} rename(p) fmt.Println(p) }func rename(p person) { p.name = "test" }
The output is Richard
because the modification to person is performed on its copy. If you want to change the value of the underlying person object, you need to use a pointer.
func main() { p := person{"Richard"} rename(&p) fmt.Println(p) }func rename(p *person) { p.name = "test" }
As above, output test
. Mutability is a situation where pointers are used in Go. Whether this is a good thing is up for debate.
API consistency
Use pointers to maintain the latest value. This keeps the API consistent even if not all methods change its value.
So, this:
func (p *person) rename(s string) { p.name = s }func (p *person) printName() { fmt.Println(p.name) }
is better than
func (p *person) rename(s string) { p.name = s }func (p person) printName() { fmt.Println(p.name) }
although for consistency there is no need to use pointers in printName
. But this will make the API simpler and avoid having to remember where exactly a reference is needed.
Indicates missing
General values, when used, have a default value of zero. But there are scenarios where you need to know that something is missing or has an unfilled value. For example, a structure contains a student's test score. If the structure is empty and has a score of 0, does it mean that the student did not do well in the test, or did not take the test at all?
The default zero value of a pointer is a nil
pointer, which means no value is set. This requirement can also be implemented as follows:
type exam struct { score int present bool }
Use a separate present
field to indicate that the student did not take the exam.
Why do I choose value?
This is somewhat subjective. Different people have different understandings of programming, so we are not required to have the same concept.
I believe it makes sense to have default values in Go as much as possible. This may not work in every situation, but in my case it prevented a big accident. Using a value instead of a pointer will not cause Tony Hoare's "million dollar mistake" due to a null pointer.
The default value of zero is useful to avoid a large number of declarations.
另一个好处是易变性造成的问题比它解决的问题多的得多。易变性给函数带来的副作用同时使得调试变得更加困难。 通过让函数返回修改之后的结构体,可以避免这种突变。
重写之前的例子
func main() { p := person{"richard"} p = rename(p) fmt.Println(p) }func rename(p person) person { p.name = "test" return p }
这也是 append
如何工作的,所以并不陌生。
x := []int{1,2} x = append(x, 3) x = append(x, 4)
鉴于指针的安全性,和值处理比指针处理更快,使用指针需要反复斟酌。
原文地址:https://medium.com/@meeusdylan/when-to-use-pointers-in-go-44c15fe04eac
译文地址:https://learnku.com/go/t/60923
The above is the detailed content of Do you know when to use Go pointers?. For more information, please follow other related articles on the PHP Chinese website!

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

闭包(closure)是一个函数以及其捆绑的周边环境状态(lexical environment,词法环境)的引用的组合。 换而言之,闭包让开发者可以从内部函数访问外部函数的作用域。 闭包会随着函数的创建而被同时创建。

本篇文章带大家了解一下golang 的几种常用的基本数据类型,如整型,浮点型,字符,字符串,布尔型等,并介绍了一些常用的类型转换操作。

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 自带的 HttpClient 的超时机制,希望对大家有所帮助。


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

Dreamweaver Mac version
Visual web development tools

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
