& and * In the middle of Go

Source: Internet
Author: User

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
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 =  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.