GoLang:为什么没有变量声明时,取地址运算符不起作用?

huangapple go评论77阅读模式
英文:

GoLang: why doesn't address-of operator work without a variable declaration

问题

在Go语言中,假设我有一个UTF-8的[]byte字节切片,我想将其作为字符串返回。

func example() *string {
   byteArray := someFunction()
   text := string(byteArray)
   return &text
}

我想消除text变量,但是Go语言不支持以下写法:

func example() *string {
   byteArray := someFunction()
   return &string(byteArray)
}

第二个例子的语法正确吗?如果正确,为什么Go语言不支持它?

英文:

In Go, suppose I have a []byte of UTF-8 that I want to return as a string.

func example() *string {
   byteArray := someFunction()
   text := string(byteArray)
   return &text
}

I would like to eliminate the text variable, but Go doesn't support the following:

func example() *string {
   byteArray := someFunction()
   return &string(byteArray)
}

Is this second example syntax correct? And if so, why doesn't Go support it?

答案1

得分: 5

因为规范是这样定义的:

> 对于类型为T的操作数x,地址操作符 &x 会生成一个类型为*T的指针,指向x。操作数必须是可寻址的,也就是说,要么是一个变量、指针间接引用或切片索引操作;要么是可寻址结构体操作数的字段选择器;要么是可寻址数组的数组索引操作。作为对可寻址要求的例外,x也可以是一个(可能带括号的)复合字面量。

请注意,类型转换(你试图使用string(byteArray)进行的操作)不包含在这个列表中。

英文:

Because the spec defines is that way:

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

Notice that type conversions (what you are trying to do with string(byteArray)) are not included in this list.

答案2

得分: 4

请注意,我将为您翻译以下内容:

查看Marc的答案以获取官方引用,但这里有一个直观的原因解释为什么Go语言不支持这个。

假设以下代码:

var myString string
stringPointer := &myString
*stringPointer = "some new value"

希望您知道,这段代码将把一个新值写入myString。这是指针的基本用法。现在考虑修改后的示例(假装它是有效的代码):

var myBytes []byte
// modify myBytes...
stringPointer := &string(myString)
*stringPointer = "some new value"

问题是,我们到底在世界上的哪个位置(或计算机)进行写入?新值将存储在哪里?

为了正确处理这种情况,编译器需要一些内部过程来将临时值“提升”为一个不可见的变量,然后取该变量的地址。这将增加不必要的复杂性,使得某些代码变得稍微更短,但同时会创建这种令人困惑的情况,即程序中存在没有明确定义位置的指针。语言不会创建这些令人困惑的幽灵变量,而是将其委托给程序员按照通常的方式使用自己的变量。

英文:

See Marc's answer for an official citation, but here's an intuitive reason for why Go doesn't support this.

Suppose the following code

var myString string
stringPointer := &myString
*stringPointer = "some new value"

Hopefully you know, this code will write some new value into myString. This is a basic use of pointers. Now consider the modified example (pretending that it is valid code):

var myBytes []byte
// modify myBytes...
stringPointer := &string(myString)
*stringPointer = "some new value"

The question is, where in the world (or computer) are we writing to?? Where is some new value going?

In order for the language to handle this correctly, the compiler would need some internal process to "promote" the temporary value to an invisible variable, and then take the address of that. This would be adding needless complexity to make some code slightly shorter, but create this confusing situation where we have pointers with no well defined location in the program. Instead of creating these confusing ghost-variables, the language delegates to the programmer to use their own variable as usual.

huangapple
  • 本文由 发表于 2021年9月14日 20:21:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/69177695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定