[英]What's the meaning of '&i' when i := &b
I'm learning the go, I tried the '&' to get the memory address.But I werid that what's the meaning of '&i',and 'i' came from i := &b , b is a int. 我正在学习编程,我尝试了'&'来获取内存地址。但是我很想知道'&i'是什么意思,而'i'来自i:=&b,b是一个整数。
b := 7
i := &b
fmt.Println(&b) //print => 0xc000088000
fmt.Println(i) //print => 0xc000088000
fmt.Println(&i) //print => 0xc00000e018
In this case , What's the meaning of '&i'? 在这种情况下,“&i”是什么意思?
&
is the address operator , evaluating it results in a memory address, which when passed to the fmt
package, usually the memory address is printed in hexadecimal format ("base 16 notation, with leading 0x"). &
是地址运算符 ,评估得出一个内存地址,该内存地址在传递给fmt
包时,通常以十六进制格式(“ base 16表示法,前导0x”)打印。
A memory address is just that: a memory address. 内存地址就是这样:一个内存地址。 It doesn't matter if it's an address of an
int
variable or a string
, or a variable of a pointer type. 不管是
int
变量或string
的地址,还是指针类型的变量的地址。 When printed, they all look the "same". 当打印时,它们看起来都“相同”。
The address operator: 地址运算符:
For an operand
x
of typeT
, the address operation&x
generates a pointer of type*T
tox
.一个操作数
x
型的T
,写入动作&x
生成类型的指针*T
到x
。
So the address operator gives you a pointer value which when you dereference, you get back the original value. 因此,地址运算符为您提供了一个指针值,当您取消引用时,该指针值将返回原始值。
&b
will be an address of the variable b
, of type *int
, which when you dereference: *b
will give you (the value of) b
. &b
将是可变的地址b
,类型*int
,其中,当取消引用: *b
会给你(的值) b
。
&i
will be the address of the variable i
, of type **int
, which when you dereference: *i
will give you the value of i
which is the address of b
. &i
是**int
类型的变量i
的地址,当您取消引用时: *i
将为您提供i
的值, i
b
的地址。 So if you also dereference that: **(&i)
, that will also give you (the value of) b
. 因此,如果您还取消引用
**(&i)
,那也将给您b
的值。
Here &b
returns address of b
and the same address is stored in i
. 这里
&b
返回的地址b
和相同的地址被存储在i
。 Since i
is also a variable &i
will return the address of variable i. 由于
i
也是变量,因此&i
将返回变量i的地址。
So &
operator generates a pointer to it's operand. 因此,
&
运算符生成一个指向其操作数的指针。 So &i
basically generates a pointer to i
which is already a pointer to b
which is nothing but a memory address. 因此
&i
基本上会生成一个指向i
的指针,该指针已经是一个指向b
的指针,而b
只是内存地址。 So when you do fmt.Println(&i)
it prints the memory address of i
. 所以,当你做
fmt.Println(&i)
它打印的内存地址i
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.