SoFunction
Updated on 2025-03-04

Study notes for Go language pointer

Go's native data types can be divided into basic types and advanced types. The basic types mainly include string, bool, int and float series. Advanced types include struct, array/slice, map, chan, func.

Compared with reference languages ​​such as Java, Python, and Javascript, Golang has the relatively ancient feature of pointers similar to C language. But unlike C language, Golang's pointers are separate types, not int types in C language, and they cannot perform integer operations on pointers. From this point of view, Golang's pointer is basically a reference.

So why does Golang need pointers? What are the unique uses of this pointer?

When learning a reference-type language, you must always figure out whether it is a value or a reference when passing a parameter to a function/method. In fact, in most cited languages, when the parameters are basic types, most of the values ​​are passed in, that is, another parameter is copied to the current function call stack. When the parameters are advanced types, the references are basically passed in. This is mainly caused by the memory management of the virtual machine.

The memory area in memory management generally includes heap and stack. Stack is mainly used to store the simple type of data used in the current call stack: string, boolean, int, float, etc. These types of memory occupies a small amount and are easy to recycle. Basically, their values ​​occupy about the same space as pointers, so they can be copied directly, and GC is also easier to optimize targetedly. Complex advanced types often occupy relatively large memory, stored in heaps, GC recycling frequency is relatively low and the cost is also high. Therefore, passing references/pointers can avoid costly copying operations, save memory, and improve program operation efficiency.

Therefore, you can consider using pointers in the following situations: 1, the value of the parameter needs to be changed; 2, avoid copying operations; 3, save memory;

Variables are convenient placeholders for reference to computer addresses, and in Go language, the corresponding memory address of a variable in the computer can be obtained through the & symbol.

package basic

import "fmt"

func main(){
  a := 1
  (&a) // 0xc4200180a0
}

A pointer variable points to the memory address of a value. The pointer syntax in Go language is similar to C++, both using * symbols to declare pointer variables;

package basic

import "fmt"

func main(){
  a := 1
  var p *int = &a
  
  ("Get the variable memory address %x\n", p) // Get the variable memory address c4200180a0  ("Get pointer variable value %v", *p) // Get the pointer variable value 1}

Although Go language has pointers, there is no pointer arithmetic, so it cannot be added or subtracted, but the pointer value can be assigned to another pointer. This is the biggest difference between pointers in Golang and pointers in C++.

Value passing? Reference pass?

When learning a reference-type language, we must first figure out whether to use pass or reference pass when passing a parameter to a function/method. In fact, most reference type languages ​​use value passing when parameters are primitive types. That is, another copy of the parameters to the current function call stack. When the parameters are advanced types, reference pass is used. This is mainly caused by the memory management of the virtual machine.

The memory area in memory management generally includes heap and stack. It is mainly used to store the simple data types used by the current call stack: string, boolean, int, float, etc. These types of memory occupies a small amount and are easy to recycle. Basically, their values ​​occupy about the same space as pointers, so they can be copied directly, and GC is also easier to optimize targetedly. Complex advanced types often occupy relatively large memory. When stored in the heap, the GC recycling rate is relatively low and the cost is also high. Therefore, passing references/pointers can avoid costly copying operations, save memory, and improve program operation efficiency.

Therefore, pointers may be considered in the following cases:

  • The value of the parameter needs to be changed;
  • Avoid copy operations;
  • Save memory;

In Golang, the specific types of struct, slice, and map are different. In fact, only the use of struct is a bit complicated. Slice, map, and chan can be used directly, without considering whether it is a value or a pointer.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.