查找常量的地址 [英] Find address of constant in go

查看:127
本文介绍了查找常量的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们写了一个程序,我们试图找到一个常量的地址。是否有可能这样做?

 包主
$ b $ func main(){
const k = 5
address:=& k
}

它给出了一个错误,谁能告诉我们如何找到一个常量的地址?

不能。



错误讯息显示:


不能接收地址k


地址运算符& 规范:地址运营商:


对于 T 类型的操作数 x ,地址操作& ; x 生成一个类型为 * T x 的指针。操作数必须是 addressable ,即变量,指针间接或片段索引操作;或可寻址结构操作数的字段选择器;或可寻址阵列的数组索引操作。作为可寻址性要求的一个例外, x 也可能是一个(可能加了括号的)复合文字。如果对 x 的评估会导致运行时恐慌,那么对& x 的评估也是如此。

常量不是列为可寻址,并且规范中没有列出可寻址(以上引用)的东西不能是地址运算符& 的操作数(不能取它们的地址)。

不允许采用常量的地址。这有两个原因:


  1. 一个常量可能根本就没有地址。
  2. 即使在运行时将一个常量值存储在内存中,这也可以帮助运行时保持常量:常量。如果可以获取一个常量值的地址,则可以将地址(指针)赋给一个变量,并且可以更改该值(指向的值,常量的值)。

如果您需要一个指向等于该常量的值的指针,请将其指定给一个可寻址的变量,以便您可以取其地址,例如

  func main(){
const k = 5
v:= k
地址:=& v // This是允许的

$ / code>

但是知道在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?

解决方案

In short: you can't.

The error message says:

cannot take the address of k

There are limitations on the operand of the address operator &. Spec: Address operators:

For an operand x of type T, the address operation &x generates a pointer of type *T to x. 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 of x 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:

  1. A constant may not have an address at all.
  2. 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).

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
}

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).

这篇关于查找常量的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆