What is a pointer

As we all know, when the program runs, the data is stored in memory. Each data stored in memory has a number, and this number is the memory address. We can find the data stored in memory based on this memory address, which can be assigned to a pointer. We can also simply think of Pointers as memory addresses.

Pointer declarations and definitions

In the Go language, to get a pointer, use the ampersand. Example:

Println("name variable value: ", name) FMT.Println("name variable value: ", name) FMT.Println("name variable value: ", name) FMT. ", nameP)} // Run result: // Name variable value: microguest bird nest // Name variable memory address: 0xc00004e240
  • The nameP pointer is of type *string
  • In the Go language,* type nameRepresents a corresponding pointer type
variable In-memory data Memory address
Name := “NEST” Micro guest bird’s nest 0xc00004e240
nameP := &name 0xc00004e240 0xc00004e360

As can be seen from the table above:

  1. The value of the common variable name is the microguest nest, which is stored in memory address 0xc00004e240
  2. The value of the pointer variable namep is the memory address of the ordinary variable 0xc00004e240
  3. The value of the nameP pointer variable is stored in memory at address 0xc00004e360
  4. Ordinary variables store data. Pointer variables store the address of the data

The var keyword declaration

We can also declare using the var keyword

var nameP *string
nameP = &name

New function declaration

nameP := new(string)
nameP = &name

You can pass the type to the built-in new function, which returns the corresponding pointer type.

Pointer manipulation

For emphasis: a pointer variable is a variable whose value is a pointer (memory address)! A pointer variable is a variable whose value is a pointer (memory address)! A pointer variable is a variable whose value is a pointer (memory address)!

  1. To get the value of the pointer variable, simply add the * sign to the pointer variable to get the corresponding data:

    NameP := *nameP fmt.println ("nameP pointer points to the value :",nameV) //nameP pointer points to the value: minivan nest
  2. Change the value to which the pointer points:

    Println("nameP pointer points to value :",*nameP) FMT.Println("name variable value :",name) // Run result: // The nameP pointer points to the value: public account: micro guest bird nest // The name variable is: public account: micro guest bird nest
  3. We see that the value to which nameP points has been changed and the value of the variable name has been changed
  4. Because the memory in which the variable name holds the data is the same memory that the pointer nameP points to. When this memory is modified by nameP, the value of the variable name is also changed.
  5. A pointer variable defined directly through the var keyword cannot be assigned because it has a value of nil, which is the memory address to which it does not point yet

    Var intP *int *intP = 10 var intP *int *intP = 10 var intP *intP = 10 Println(":::",intP) int int = new(int) int int = new(int) int int = new(int) int int = new(int) int int = new(int) 0xc0000ac088 FMT.Println(*intP) //66 // var intP := new(int) *intP=66

Pointer parameter

When we use a pointer as an argument to a function, we can change the value of an argument within the function by using a parameter:

Func main() {name := "clean" FMT.Println("name is :",name)} func modify(name *string) {*name = "wucs"} // The value of name is: wucs

Pointer receiver

  1. If the receiver type is a reference type such as map, slice, channel, etc., no pointer is used.
  2. If you need to modify the receiver, you need to use a pointer;
  3. If the recipient is a large type, consider using Pointers, since memory copies are cheap and therefore efficient.

When are Pointers used

  • Do not use Pointers to reference types such as map, slice, and channel.
  • Pointers are used if data or state inside the method receiver needs to be modified.
  • If you need to modify the value of the parameter or internal data, you also need to use pointer type parameters;
  • If the structure is large and requires a copy of memory every time an argument is passed or a method is called, consider using Pointers.
  • Small data types such as int and bool do not need Pointers;
  • If you need concurrency security, do not use Pointers as much as possible, and use Pointers to ensure concurrency security;
  • Pointers should not be nested, that is, you should not use a pointer to a pointer, although the Go language allows you to do this, but this can make your code very complicated.