Home > Backend Development > Golang > Introduction to Go pointers

Introduction to Go pointers

藏色散人
Release: 2020-10-26 14:02:46
forward
2059 people have browsed it

The following column will introduce Go pointers to you in the golang tutorial column. I hope it will be helpful to friends in need!

Combined with this example to analyze

The result:

Combined with the basis of the previous C language, Draw a picture to explain why the above values ​​appear. Let’s first check what these two operators are in Go.

①For all results with a

var a int = 1 defines a variable a with a value of 1, as shown in the following figure:

&a is the address that stores the value of a variable

*&a is a pointer to &a, *&a = a = 1

②All Result with b

var b *int = &a

Similar to C language int *b = &a Define a pointer b pointing to an integer variable, b points to the address of a

So:

b = &a = 0xc04203c1d0

&b = 0xc042058018 &b is the real address of the pointer variable b, which is the one after execution

*&b = *(&b) = b = &a = 0xc04203c1d0

*b = *&a = a = 1

③All results with c

As shown in the picture above:

var c **int = &b

actually defines a pointer variable pointing to an integer pointer

c = &b = 0xc042058018

*c = *&b = b = &a = 0xc04203c1d0

&c is the real address of this variable. What is shown here is the result of the operation 0xc042068020

* &c With experience, you can find that *& is eliminated directly = c = 0xc042058018

**c At first glance, you don’t know what it is. In fact, you can evaluate it through conversion

**c = **&b = *b = *&a = a = 1

***&*&*&*&*&c This can be eliminated first*& **c = 1

④About x

x = *b = *&a = a = 1

To summarize the above:

##&Passed The variable takes the address * by taking the address value

&a to get the address first, and then "* xxx address" gets the value of the variable at the address, that is, a

*& can offset

The above is the detailed content of Introduction to Go pointers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template