在go中找到常量的地址 [英] Find address of constant in go
问题描述
我们编写了一个程序,通过它我们试图找到一个常量的地址.可以这样吗?
We have written one program by which we try to find an address of a constant. Is it possible to do it like that?
package main
func main() {
const k = 5
address := &k
}
它给出了一个错误,谁能告诉我们如何找到一个常量的地址?
It gives an error, can anyone tell how can we find the address of a constant?
推荐答案
简而言之:你不能.
错误信息说:
不能取k的地址
地址运算符&
的操作数有限制.规范:地址操作符:
There are limitations on the operand of the address operator &
. Spec: Address operators:
对于T
类型的操作数x
,地址操作&x
生成*T类型的指针代码> 到
x
.操作数必须是可寻址,即变量、指针间接或切片索引操作;或可寻址结构操作数的字段选择器;或可寻址数组的数组索引操作.作为可寻址性要求的一个例外,x
也可以是(可能带括号的)复合字面意思.如果 x
的评估会导致 运行时恐慌,那么&x
的评估也是如此.
For an operand
x
of typeT
, the address operation&x
generates a pointer of type*T
tox
. The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement,x
may also be a (possibly parenthesized) composite literal. If the evaluation ofx
would cause a run-time panic, then the evaluation of&x
does too.
常量不列为可寻址,规范中未列为可寻址的东西(上面引用)不能 是地址运算符 &
的操作数(不能取它们的地址).
Constants are not listed as addressable, and things that are not listed in the spec as addressable (quoted above) cannot be the operand of the address operator &
(you can't take the address of them).
不允许取常量的地址.这有两个原因:
It is not allowed to take the address of a constant. This is for 2 reasons:
- 常量可能根本没有地址.
- 即使在运行时将常量值存储在内存中,这也是为了帮助运行时保持常量:constant.如果可以获取常量值的地址,则可以将地址(指针)分配给变量,然后可以更改它(指向的值,常量的值).Robert Griesemer(Go 的作者之一)写了为什么不允许使用字符串文字的地址:如果你可以获取字符串常量的地址,你可以调用一个函数 [分配给指向的值导致]可能会产生奇怪的效果 - 您当然不希望文字字符串常量发生变化."(来源)
- A constant may not have an address at all.
- And even if a constant value is stored in memory at runtime, this is to help the runtime to keep constants that: constant. If you could take the address of a constant value, you could assign the address (pointer) to a variable and you could change that (the pointed value, the value of the constant). Robert Griesemer (one of Go's authors) wrote why it's not allowed to take a string literal's address: "If you could take the address of a string constant, you could call a function [that assigns to the pointed value resulting in] possibly strange effects - you certainly wouldn't want the literal string constant to change." (source)
如果您需要一个指向等于该常量的值的指针,请将其分配给一个可寻址的变量,以便您可以获取其地址,例如
If you need a pointer to a value being equal to that constant, assign it to a variable of which is addressable so you can take its address, e.g.
func main() {
const k = 5
v := k
address := &v // This is allowed
}
但是要知道,在 Go 中,数字常量表示任意精度的值并且不会溢出.当您将常量的值分配给变量时,它可能是不可能的(例如,常量可能大于您分配给它的变量类型的最大值 - 导致编译时错误),或者它可能不一样(例如在浮点常量的情况下,它可能会丢失精度).
But know that in Go numeric constants represent values of arbitrary precision and do not overflow. When you assign the value of a constant to a variable, it may not be possible (e.g. the constant may be greater than the max value of the variable's type you're assigning it to - resulting in compile-time error), or it may not be the same (e.g. in case of floating point constants, it may lose precision).
这篇关于在go中找到常量的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!