Look at a piece of code, run it manually, see how many problems you can do right?
package main
import "fmt"
func main() {
var a int = 1
var b *int = &a
var c **int = &b
var x int = *b
fmt.Println("a = ",a)
fmt.Println("&a = ",&a)
fmt.Println("*&a = ",*&a)
fmt.Println("b = ",b)
fmt.Println("&b = ",&b)
fmt.Println("*&b = ",*&b)
fmt.Println("*b = ",*b)
fmt.Println("c = ",c)
fmt.Println("*c = ",*c)
fmt.Println("&c = ",&c)
fmt.Println("*&c = ",*&c)
fmt.Println("**c = ",**c)
fmt.Println("***&*&*&*&c = ",***&*&*&*&*&c)
fmt.Println("x = ",x)
}
Copy the code
The symbol & means to take the address of a variable
For example, the address of variable A is &a
The * symbol means to evaluate a pointer,
For example, *&a is the value of the address of the variable A, of course, is the value of a
Simple explanation
* and ampers& cancel each other out but notice that they cancel out, but they don't. A and ampers& are the same thing, they're both values of A, and they're 1 (because they cancel each other out). A and ampers& are the same thing, ** a is the same as *&a and ** a is the same as *&b. **c is the same as **&b. **c is the same as a and b, so **c is the same as a and B, so **c is the same as a and B, so **c is the same as a and BCopy the code
Try running it
## Run result
$ go run main.go
a = 1
&a = 0xc200000018
*&a = 1
b = 0xc200000018
&b = 0xc200000020
*&b = 0xc200000018
*b = 1
c = 0xc200000020
*c = 0xc200000018
&c = 0xc200000028
*&c = 0xc200000020
**c = 1
***&*&*&*&c = 1
x = 1
Copy the code
Two symbols cancel out the order
*& can be cancelled out at any time, but the ampersand cannot be cancelled out because of the wrong order."*&a\t=\t",*&a) // Successfully cancels out and prints out the value of a fmt.println ("&*a\t=\t",&*a) // Cannot cancel, an error will be reportedCopy the code