Package MainImport "FMT"FuncMain() {var a int =1var b *int = &aVarc **int = &bvar 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)}
Explanation theory
&
The meaning of the symbol is to take the address of the variable, such as: a
the address of the variable is&a
*
The symbol means that the value of the pointer, such as: *&a
, is the value of the a
address of the variable, of course, a
the value of
A simple explanation
*
And &
can counteract each other, while note that *&
can be offset out, but &*
is not to offset the
a
And *&a
is the same, are all a value, the value is 1 (because they *&
cancel each other off)
The a
same, and *&*&*&*&a
is the same, are 1 (because 4 of them *&
cancel each other off)
Expand
Because there are
var b *int = &a
So
a
And the same as, are the *&a
*b
value of a, the value is 1 ( b
&a
See)
Expand again
Because there are
var c **int = &b
So
**c
And **&b
is the same, put & about to go after
Will find **c
that the ' B 是一样的 (从这里也不难看出,
c 和
也是一样的) 又因为上面得到的
b&a 和
b 是一样的 所以
**c 和
&a 是一样的,再次把*&约去后
**c 和
A ' is the same, It's all 1.
Do you have a try?
Announcement results
The value of the address within the result of the run (beginning with 0xc200) may vary depending on the machine running, you know
$ go run main.go a = 1&a = 0xc200000018*&a = 1b = 0xc200000018&b = 0xc200000020*&b = 0xc200000018*b = 1c = 0xc200000020*c = 0xc200000018&c = 0xc200000028*&c = 0xc200000020**c = 1***&*&*&*&c = 1x = 1
Two order of sign cancellation
*&
Can be offset at any time, but &*
cannot be offset, because the order is not correct
fmt.Println("*&a\t=\t",*&a) //成功抵消掉,打印出1,即a的值fmt.Println("&*a\t=\t",&*a) //无法抵消,会报错
& and * In the middle of Go