What is a pointer
As we all know, the data stored in memory when the program is running, each data stored in memory has a number, this number is the memory address. We can use this memory address to find the data stored in memory, and the memory address can be assigned to a pointer. We can also simply think of Pointers as memory addresses.
Declaration and definition of Pointers
In Go, to get a pointer, you simply use the address character &. Example:
func main(a) {
name := "A little bird's nest."
nameP := &name / / get the address
fmt.Println("The name variable value is:", name)
fmt.Println(The memory address of the name variable is:, nameP)
}
// Run result:
// The value of the name variable is: micro guest bird nest
// The memory address of the name variable is 0xc00004e240
Copy the code
- The nameP pointer is of type *string
- In the Go language,
* type name
Represents a corresponding pointer type
variable | In-memory data | Memory address |
---|---|---|
Name := “” | Micro guest bird’s nest | 0xc00004e240 |
nameP := &name | 0xc00004e240 | 0xc00004e360 |
As can be seen from the table above:
- The value of the common variable name is a micro guest nest, stored in memory address 0xC00004E240 memory
- The value of the pointer variable namep is the memory address of the common variable 0xc00004e240
- The value of the pointer variable nameP is stored in the memory address 0xC00004e360
- Ordinary variables store data, pointer variables store the address of data
Var indicates the keyword
We can also declare using the var keyword
var nameP *string
nameP = &name
Copy the code
New function declaration
nameP := new(string)
nameP = &name
Copy the code
You can pass the type to this built-in new function, and it will return the corresponding pointer type.
Operations on Pointers
A pointer variable is a variable whose value is a pointer (memory address). A pointer variable is a variable whose value is the pointer (memory address)! A pointer variable is a variable whose value is the pointer (memory address)!
- Get the value to which the pointer points:
Just add * to the pointer variable to get the data corresponding to the pointer variable value:
nameV := *nameP
fmt.Println("The nameP pointer points to the value :",nameV) // The nameP pointer points to the value of the micro guest bird nest
Copy the code
- Modify the value to which the pointer points:
*nameP = "Official Account: Weiguest Bird's Nest" // Modify the value to which the pointer points
fmt.Println("The nameP pointer points to the value :",*nameP)
fmt.Println(The value of the name variable is:,name)
// Run result:
// The nameP pointer points to the following value: public account: micro guest bird nest
// The value of the name variable is: public account: micro guest bird nest
Copy the code
- We find that the value to which the nameP pointer points has been changed, and the value of the variable name has also been changed
- Because the name variable stores data in the same memory as the pointer to nameP, when nameP changes the memory, the value of the variable name is also changed.
- A pointer variable defined directly by the var keyword cannot be assigned because it has a nil value, that is, a memory address not yet pointed to
// Error examples
var intP *int
*intP = 10 // Error: we should allocate a block of memory first, the memory address as the value of the variable intP, this memory can hold 10.
// Should be used
var intP *int // Declare a pointer variable of type int intP
intP = new(int) // Allocate a block of memory to the pointer
*intP = 66
fmt.Println(": : :",intP) //::: 0xc0000ac088
fmt.Println(*intP) / / 66
// Short form
var intP := new(int)
*intP=66
Copy the code
Pointer parameter
When we give a function a pointer as an argument, we can change the value of the argument in the function through the parameter:
func main(a) {
name := "Clean"
modify(&name)
fmt.Println("The value of name is :",name)
}
func modify(name *string) {
*name = "wucs"
}
// Run result:
// The value of name is wucs
Copy the code
Pointer receiver
- If the receiver type is a reference type such as Map, slice, or channel, no pointer is used.
- If you need to change the receiver, you need to use a pointer.
- If the recipient is a larger type, consider using Pointers because memory copying is cheaper and therefore more efficient.
When to use Pointers
- Do not use Pointers to reference types such as Map, slice, channel;
- If you need to modify the data or state inside the method receiver, you need to use Pointers.
- If you need to change the parameter value or internal data, you also need to use the pointer type parameter.
- If it is a large structure, memory needs to be copied every time an argument is passed or a method is called.
- There is no need to use Pointers for small data types like int and bool.
- If concurrency security is required, do not use Pointers as much as possible. Use Pointers to ensure concurrency security.
- Pointers are best not nested, that is, don’t use a pointer to a pointer, although Go allows this, but it can make your code very complicated.