SoFunction
Updated on 2025-03-05

When should I use pointers in Go language

What is a pointer

We all know that the data at the time of the program is stored in memory, and each data stored in memory has a number, which is the memory address. We can find the data stored in memory based on this memory address, and the memory address can be assigned to a pointer. We can also simply understand that pointers are memory addresses.

Declaration and definition of pointers

In Go language, get a pointer and use the address picker & directly.
Example:

func main() {
  name := "Weike Bird's Nest"
  nameP := &name //Get the address  ("The value of the name variable is:", name)
  ("The memory address of the name variable is:", nameP)
}
//Run result://The value of the name variable is: Weike Bird's Nest//nameThe memory address of the variable is: 0xc00004e240

The nameP pointer type is *string
In Go language, the * type name represents a corresponding pointer type

variable Data in memory Memory address
name := "Weike Bird's Nest" Weike Bird's Nest 0xc00004e240
nameP := &name 0xc00004e240 0xc00004e360

From the table above, you can see:

  • The value of the ordinary variable name is Weike Bird's Nest and is stored in memory with the memory address 0xc00004e240
  • The value of the pointer variable namep is the memory address of the normal variable 0xc00004e240
  • The value of the pointer variable nameP is stored in memory with the memory address 0xc00004e360
  • Normal variables store data, pointer variables store data address

var keyword statement

We can also use the var keyword declaration

var nameP *string
nameP = &name

new function declaration

nameP := new(string)
nameP = &name

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

Pointer operation

Here is a point of emphasis:

A pointer variable is a variable, and the value of this variable is a pointer (memory address)!
A pointer variable is a variable, and the value of this variable is a pointer (memory address)!
A pointer variable is a variable, and the value of this variable is a pointer (memory address)!

Get the value pointed to by the pointer:

Just add the * sign to the pointer variable to get the data corresponding to the pointer variable value:

nameV := *nameP
("The value pointed to by the nameP pointer is:",nameV) //namePThe value pointed to by the pointer is: Weike Bird's Nest

Modify the value pointed to by the pointer:

*nameP = "Official Account: Weike Bird's Nest" //Modify the value pointed to by the pointer("The value pointed to by the nameP pointer is:",*nameP)
("The value of the name variable is:",name)
//Run result://The value pointed to by the nameP pointer is: Official account: Weike Bird's Nest//nameThe value of the variable is: Official account:Weike Bird's Nest
  • We found that the value pointed to by the nameP pointer has been changed, and the value of the variable name has also been changed
  • Because the memory in which the variable name stores data is the memory pointed to by the pointer nameP, after this memory is modified by nameP, the value of the variable name is also modified.

Pointer variables directly defined by the var keyword cannot be assigned because their value is nil, which means there is no memory address pointed to yet

//Error Examplevar intP *int
*intP = 10  //Error, you should first allocate a piece of memory, and the memory address is the value of the variable intP, so this memory can be stored 10.
//It should be usedvar intP *int  //Declare the pointer variable of int type intPintP = new(int) // Allocate a piece of memory to the pointer*intP = 66 
(":::",intP)  //::: 0xc0000ac088
(*intP) //66
//Short writingvar intP := new(int)
*intP=66

Pointer Parameters

When using a pointer as a parameter to a function, you can change the value of the actual parameter through formal parameters in the function:

func main() {
    name := "Dust-free"
    modify(&name)
    ("The value of name is:",name)
}
func modify(name *string)  {
    *name = "wucs"
}
//Run result://nameThe value of: wucs

Pointer Receiver

  • If the receiver type is a reference type such as map, slice, channel, etc., no pointers are used;
  • If the receiver needs to be modified, then the pointer needs to be used;
  • If the receiver is of a relatively large type, you can consider using pointers, because the memory copy is cheap and therefore efficient.

Under what circumstances do you use pointers

  • Do not use pointers for reference types such as map, slice, channel;
  • If you need to modify the data or status within the method recipient, you need to use a pointer;
  • If you need to modify the value of the parameter or internal data, you also need to use pointer-type parameters;
  • If it is a relatively large structure, each parameter passing or calling method requires a memory copy, which consumes a lot of memory. You can consider using pointers at this time;
  • Small data types like int and bool do not need to use pointers;
  • If concurrency security is required, do not use pointers as much as possible. Use pointers to ensure concurrency security;
  • It is best not to nest pointers, that is, do not use a pointer to a pointer. Although Go allows this, this will make your code extremely complicated.

This is the end of this article about how to use pointers in Go. For more related Go language pointer content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!