星号在“Go”中起什么作用?
我一直在浏览并尝试理解 Go 网站 上的示例,并且不断遇到像这样的例子中的特殊星号字符:
s := "hello"
if s[1] != 'e' {
os.Exit(1)
}
s = "good bye"
var p *string = &s
*p = "ciao"
另外,我刚刚注意到,&s
是什么?是通过引用赋值吗(我可能在这里使用 PHP 谈话)?
I've been going through and trying to understand the examples on the Go website and I keep coming across a special asterisk character in examples like this:
s := "hello"
if s[1] != 'e' {
os.Exit(1)
}
s = "good bye"
var p *string = &s
*p = "ciao"
Also, I just noticed, what's with the &s
? Is it assignment by reference (I might be using PHP talk here)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
附加到类型 (
*string
) 的*
表示指向该类型的指针。在赋值中附加到变量的
*
(*v = ...
) 表示间接赋值。即改变变量指向的值。附加到变量或表达式 (
*v
) 的*
表示指针取消引用。也就是说,获取变量指向的值。附加到变量或表达式 (
&v
) 的&
表示引用。也就是说,创建一个指向变量值或字段的指针。*
attached to a type (*string
) indicates a pointer to the type.*
attached to a variable in an assignment (*v = ...
) indicates an indirect assignment. That is, change the value pointed at by the variable.*
attached to a variable or expression (*v
) indicates a pointer dereference. That is, take the value the variable is pointing at.&
attached to a variable or expression (&v
) indicates a reference. That is, create a pointer to the value of the variable or to the field.我猜这与 C 中的意思相同
p 是指向字符串的指针
语句
var p *string = &s
将分配s 的地址
对象为p
下一行
*p = "ciao"
将更改s
的内容请参阅 语言设计常见问题解答
有趣的是,没有指针算术
Im guessing it means the same as in C
p is a pointer to a string
The statement
var p *string = &s
would assign the address of thes
object top
Next line
*p = "ciao"
would change the contents ofs
See this link from the Language Design FAQ
Interestingly, no pointer arithmetic
Go lang 地址、指针和类型:
所有这些Go 变量有一个内存地址。它们之间的区别是字符串类型保存字符串值,int类型保存整数值,而指针类型保存地址。因此,即使保存地址的变量也有自己的内存地址。
指针:
在变量名称计算其地址之前添加
&
。或者想,“这是我的地址,这样你就知道在哪里可以找到我。”在指针变量之前添加
*
可取消对指向其所保存地址的指针的引用。或者想想,“将操作传递到我的值的地址。”在这里玩:http://play.golang.org/p/u3sPpYLfz7
Go lang Addresses, Pointers and Types:
All these Go variables have an address in memory. The distinction between them is string types hold string values, int types hold integer values, and pointer types hold addresses. So even variables that hold addresses have their own memory address.
Pointers:
Adding
&
before a variable name evaluates to it's address. Or think, "here's my address so you know where to find me."Adding
*
before a pointer variable dereferences the pointer to the address it is holding. Or think, "pass the action on to the address which is my value."Go Play here: http://play.golang.org/p/u3sPpYLfz7
我就是这么看的。不同的措辞可能有助于某人更好地理解它(您可以复制粘贴该代码并检查输出):
That's how I see it. Different phrasing might help someone to understand it better (you can copy paste that code and examine the output):
*
字符用于在 C 和 Go 中定义指针。该变量不是实际值,而是具有值所在位置的地址。&
运算符用于获取对象的地址。The
*
character is used to define a pointer in both C and Go. Instead of a real value the variable instead has an address to the location of a value. The&
operator is used to take the address of an object.我不懂Go,但根据语法,它似乎类似于C - 那是一个指针。它与参考类似,但级别较低且功能更强大。它包含相关项目的内存地址。
&a
获取变量的内存地址,而*a
取消引用它,获取内存地址处的值。另外,声明中的
*
表示它是一个指针。所以,是的,就像在 PHP 中一样,
s
的值发生了变化,因为p
和&s
指向同一块内存。I don't know Go, but based on the syntax, it seems that its similar to C - That is a pointer. Its similar to a reference, but lower level and more powerful. It contains the memory address of the item in question.
&a
gets the memory address of a variable and*a
dereferences it, getting the value at the memory address.Also, the
*
in the declaration means that it is a pointer.So yes, its like in PHP in that the value of
s
is changed becausep
and&s
point to the same block of memory.golang中的*作为指针。指针保存一个值的内存地址。
*T 类型是指向 T 值的指针。它的零值为零。
的&运算符生成一个指向其操作数的指针。
* 运算符表示指针的基础值。
这称为“解除引用”或“引导”。
与 C 不同,Go 没有指针运算。
The * in golang as pointers. A pointer holds the memory address of a value.
The type *T is a pointer to a T value. It's zero value is nil.
The & operator generates a pointer to its operand.
The * operator denotes the pointer's underlying value.
This is known as "dereferencing" or "in directing".
Unlike C, Go has no pointer arithmetic.