Do you know when to use Go pointers?
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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



In Go, WebSocket messages can be sent using the gorilla/websocket package. Specific steps: Establish a WebSocket connection. Send a text message: Call WriteMessage(websocket.TextMessage,[]byte("Message")). Send a binary message: call WriteMessage(websocket.BinaryMessage,[]byte{1,2,3}).

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

Go and the Go language are different entities with different characteristics. Go (also known as Golang) is known for its concurrency, fast compilation speed, memory management, and cross-platform advantages. Disadvantages of the Go language include a less rich ecosystem than other languages, a stricter syntax, and a lack of dynamic typing.

Memory leaks can cause Go program memory to continuously increase by: closing resources that are no longer in use, such as files, network connections, and database connections. Use weak references to prevent memory leaks and target objects for garbage collection when they are no longer strongly referenced. Using go coroutine, the coroutine stack memory will be automatically released when exiting to avoid memory leaks.

Unit testing concurrent functions is critical as this helps ensure their correct behavior in a concurrent environment. Fundamental principles such as mutual exclusion, synchronization, and isolation must be considered when testing concurrent functions. Concurrent functions can be unit tested by simulating, testing race conditions, and verifying results.

By using pointers and references, memory usage in C++ can be optimized: Pointers: store addresses of other variables and can point to different variables, saving memory, but may generate wild pointers. Reference: Aliased to another variable, always points to the same variable, does not generate wild pointers, and is suitable for function parameters. Optimizing memory usage can improve code efficiency and performance by avoiding unnecessary copies, reducing memory allocations, and saving space.

In Golang, error wrappers allow you to create new errors by appending contextual information to the original error. This can be used to unify the types of errors thrown by different libraries or components, simplifying debugging and error handling. The steps are as follows: Use the errors.Wrap function to wrap the original errors into new errors. The new error contains contextual information from the original error. Use fmt.Printf to output wrapped errors, providing more context and actionability. When handling different types of errors, use the errors.Wrap function to unify the error types.

When passing a map to a function in Go, a copy will be created by default, and modifications to the copy will not affect the original map. If you need to modify the original map, you can pass it through a pointer. Empty maps need to be handled with care, because they are technically nil pointers, and passing an empty map to a function that expects a non-empty map will cause an error.
