This is a creation in Article, where the information may have evolved or changed.
Look at a piece of code first
First put a piece of code, manually run it, to see if you can do the right several questions?
Package MainImport"FMT" Func Main () {var aint =1 Var b *int = &a var c * *int = &b var xint = *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)}
Explain
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
aAnd *&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
aAnd 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
**cAnd **&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 = Span class= "hljs-number" >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) //无法抵消,会报错