I'm just working through the Go tour, and I'm confused about pointers and interfaces. Why doesn't this Go code compile?
我只是在进行巡回演讲,我对指针和接口感到困惑。为什么不进行代码编译呢?
package main
type Interface interface {}
type Struct struct {}
func main() {
var ps *Struct
var pi *Interface
pi = ps
_, _ = pi, ps
}
i.e. if Struct
is an Interface
, why wouldn't a *Struct
be a *Interface
?
也就是说,如果Struct是一个接口,为什么一个*Struct不是一个*接口呢?
The error message I get is:
我得到的错误信息是:
prog.go:10: cannot use ps (type *Struct) as type *Interface in assignment:
*Interface is pointer to interface, not interface
2 个解决方案
#1
151
When you have a struct implementing an interface, a pointer to that struct implements automatically that interface too. That's why you never have *SomeInterface
in the prototype of functions, as this wouldn't add anything to SomeInterface
, and you don't need such a type in variable declaration (see this related question).
当您有一个实现一个接口的struct时,一个指向该结构的指针也会自动实现该接口。这就是为什么在函数原型中没有*SomeInterface的原因,因为这不会给某个接口添加任何东西,而且在变量声明中也不需要这样的类型(请参阅相关的问题)。
An interface value isn't the value of the concrete struct (as it has a variable size, this wouldn't be possible), but it's a kind of pointer (to be more precise a pointer to the struct and a pointer to the type). Russ Cox describes it exactly here :
一个接口值不是具体结构的值(因为它有一个可变的大小,这是不可能的),但是它是一种指针(更精确地指向结构的指针和指向类型的指针)。Russ Cox在这里详细描述了它:
Interface values are represented as a two-word pair giving a pointer to information about the type stored in the interface and a pointer to the associated data.
接口值表示为两个字对,它提供了一个指针,用于指向存储在接口中的类型的信息,以及指向相关数据的指针。
This is why Interface
, and not *Interface
is the correct type to hold a pointer to a struct implementing Interface
.
这就是为什么接口,而不是*接口是一个正确的类型来持有一个指向struct实现接口的指针。
So you must simply use
所以你必须简单地使用。
var pi Interface
#2
3
This is perhaps what you meant:
这也许就是你的意思:
package main
type Interface interface{}
type Struct struct{}
func main() {
var ps *Struct
var pi *Interface
pi = new(Interface)
*pi = ps
_, _ = pi, ps
}
Compiles OK. See also here.
编译好了。参见这里。
#1
151
When you have a struct implementing an interface, a pointer to that struct implements automatically that interface too. That's why you never have *SomeInterface
in the prototype of functions, as this wouldn't add anything to SomeInterface
, and you don't need such a type in variable declaration (see this related question).
当您有一个实现一个接口的struct时,一个指向该结构的指针也会自动实现该接口。这就是为什么在函数原型中没有*SomeInterface的原因,因为这不会给某个接口添加任何东西,而且在变量声明中也不需要这样的类型(请参阅相关的问题)。
An interface value isn't the value of the concrete struct (as it has a variable size, this wouldn't be possible), but it's a kind of pointer (to be more precise a pointer to the struct and a pointer to the type). Russ Cox describes it exactly here :
一个接口值不是具体结构的值(因为它有一个可变的大小,这是不可能的),但是它是一种指针(更精确地指向结构的指针和指向类型的指针)。Russ Cox在这里详细描述了它:
Interface values are represented as a two-word pair giving a pointer to information about the type stored in the interface and a pointer to the associated data.
接口值表示为两个字对,它提供了一个指针,用于指向存储在接口中的类型的信息,以及指向相关数据的指针。
This is why Interface
, and not *Interface
is the correct type to hold a pointer to a struct implementing Interface
.
这就是为什么接口,而不是*接口是一个正确的类型来持有一个指向struct实现接口的指针。
So you must simply use
所以你必须简单地使用。
var pi Interface
#2
3
This is perhaps what you meant:
这也许就是你的意思:
package main
type Interface interface{}
type Struct struct{}
func main() {
var ps *Struct
var pi *Interface
pi = new(Interface)
*pi = ps
_, _ = pi, ps
}
Compiles OK. See also here.
编译好了。参见这里。