Go language Pointers to study notes

  • 2020-06-19 10:31:03
  • OfStack

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

Compared to Java, Python, Javascript and other languages with reference types, Golang has the relatively old property of having Pointers similar to C. But unlike C, Golang's Pointers are of a separate type, not the int type in C, and they cannot be evaluated as integers. At this point, the pointer to Golang is basically a reference.

So why does Golang need Pointers? What unique use could this pointer have?

When learning reference-type languages, always make sure that when you pass arguments to a function/method, you pass in a value or a reference. In fact, in most reference languages, when an argument is a primitive type, the value is passed in, which means another copy of the argument is added to the current function call stack. When the argument is of a high-level type, basically all you pass in is a reference. This is mainly due to the memory management of the virtual machine.

Memory area 1 in memory management generally includes heap and stack. stack is mainly used to store the simple type data used by the current call stack: string, boolean, int, float, etc. These types have a small footprint, are easy to recycle, and basically have the same value as Pointers, so they can be copied directly, and GC is easier to optimize. Complex advanced types tend to take up relatively large amounts of memory, stored in heap, GC recovery frequency is relatively low, and the cost is also high, so passing reference/pointer can avoid costly replication operation, and save memory, improve the program running efficiency.

Therefore, you can consider using a pointer in the following cases: 1, you need to change the value of the parameter; 2. Avoid copying operation; 3, save memory;

A variable is a handy placeholder for referring to a computer address that is available in the Go language & Symbol gets the memory address of a variable in the computer.


package basic

import "fmt"

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

A pointer variable points to the memory address of a value. Pointer syntax in Go language is similar to C++, which USES * symbol to declare pointer variables.


package basic

import "fmt"

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

The Go language has Pointers, but no Pointers count. You can't add or subtract them, but you can assign a pointer value to another pointer. This is the biggest difference between Pointers in Golang and Pointers in C++.

Value delivery? Passing by reference?

When studying reference-type languages, we first need to make sure that when passing arguments to a function/method, we use pass or pass by reference. In fact, most reference type languages use value passing when arguments are primitive types. This is another copy of the argument to the current function call stack. When the parameter is a high-level type, reference passing is used. This is mainly due to the memory management of the virtual machine.

Memory area 1 in memory management generally includes heap (heap) and stack (stack), mainly used to store the simple data types used by the current call stack: string, boolean, int, float, etc. These types of memory are small, easy to recycle, and basically have the same value as Pointers, so they can be copied directly, and GC is easier to optimize. Complex advanced types tend to take up relatively large amounts of memory and are stored in the heap (heap). The GC recovery rate is relatively low and the cost is high, so passing reference/pointer can avoid costly replication operations and save memory and improve program efficiency.

Therefore, consider using Pointers in the following situations:

You need to change the value of the parameter; Avoid copying operations; Save memory;

In Golang, specific to the advanced types struct, slice, map are also different. In fact, only struct is a little more complicated to use, slice, map, chan can be used directly, regardless of the value or pointer.


Related articles: