为什么在Go语言中无法获取类型转换的地址?

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

Why can't I get the address of a type conversion in Go?

问题

当我编译这段代码时,编译器告诉我无法获取str(s)的地址

func main() {
    s := "hello, world"
    type str string
    sp := &str(s)
}

所以我的问题是,类型转换是否会寻找一个新的地址来定位当前的新s,或者还有其他我没有考虑到的情况吗?

英文:

When I compile this code, the compiler tells me that I cannot take the address of str(s).

func main() {
	s := "hello, world"
	type str string
	sp := &str(s)
}

So my question is whether a type conversion may look for a new address to locate the current new s, or something else that I haven't thought of?

答案1

得分: 8

《Go编程语言规范》

表达式

表达式通过将运算符和函数应用于操作数来指定值的计算。

转换

转换是形式为T(x)的表达式,其中T是类型,x是可以转换为类型T的表达式。

地址运算符

对于类型为T的操作数x,地址操作符&x生成一个指向x的类型为*T的指针。操作数必须是可寻址的,即变量、指针间接引用或切片索引操作;或者是可寻址结构操作数的字段选择器;或者是可寻址数组的数组索引操作。作为对可寻址要求的例外,x也可以是一个(可能带括号的)复合字面量。如果对x的求值会导致运行时恐慌,则对&x的求值也会导致恐慌。

表达式是临时的、短暂的值。表达式值没有地址。它可以存储在寄存器中。转换是一种表达式。例如,

package main

import (
	"fmt"
)

func main() {
	type str string
	s := "hello, world"
	fmt.Println(&s, s)

	// 错误:无法取 str(s) 的地址
	sp := &str(s)
	fmt.Println(sp, *sp)
}

输出:

main.go:13:8: 无法取 str(s) 的地址

要使值可寻址,它必须是持久的,就像一个变量一样。例如,

package main

import (
	"fmt"
)

func main() {
	type str string
	s := "hello, world"
	fmt.Println(&s, s)

	ss := str(s)
	sp := &ss
	fmt.Println(sp, *sp)
}

输出:

0x1040c128 hello, world
0x1040c140 hello, world
英文:

> The Go Programming Language Specification
>
> Expressions
>
> An expression specifies the computation of a value by applying
> operators and functions to operands.
>
> Conversions
>
> Conversions are expressions of the form T(x) where T is a type and x
> is an expression that can be converted to type T.
>
> 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.

Expressions are temporary, transient values. The expression value has no address. It may be stored in a register. A comversion is an expression. For example,

package main

import (
	"fmt"
)

func main() {
	type str string
	s := "hello, world"
	fmt.Println(&s, s)

	// error: cannot take the address of str(s)
	sp := &str(s)
	fmt.Println(sp, *sp)
}

Output:

main.go:13:8: cannot take the address of str(s)

To be addressable a value must be persistent, like a variable. For example,

package main

import (
	"fmt"
)

func main() {
	type str string
	s := "hello, world"
	fmt.Println(&s, s)

	ss := str(s)
	sp := &ss
	fmt.Println(sp, *sp)
}

Output:

0x1040c128 hello, world
0x1040c140 hello, world

huangapple
  • 本文由 发表于 2017年8月31日 10:44:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/45972425.html
匿名

发表评论

匿名网友

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

确定