英文:
Why can't I use a pointer to a specific type where *interface{} is expected?
问题
我有以下函数:
func bytesToData(data interface{}, b []byte) error {
buf := bytes.NewBuffer(b)
dec := gob.NewDecoder(buf)
return dec.Decode(data)
}
我使用这个函数在 boltdb 中获取结构体数据。我想要将函数签名改为:
func bytesToData(data *interface{}, b []byte) error
然后我想要像这样调用它(在这种情况下,b
是一个 gob 编码的 Account
):
acc := &Account{}
err := bytesToData(acc, b)
但是当我这样做时,我会得到一个错误,类似于 Cannot use *Account for type *interface{}
。
目前,我只是将它改回了 interface{}
。但是,如果我直接传入一个 Account
或其他类型而不将其作为指针,gob 会抛出一个错误。这似乎应该在编译时可以检查到。而且,鉴于类型 interface{}
的参数可以接受任何类型,为什么类型 *interface{}
的参数不接受任何类型的指针呢?
英文:
I have the following function:
func bytesToData(data interface{}, b []byte) error {
buf := bytes.NewBuffer(b)
dec := gob.NewDecoder(buf)
return dec.Decode(data)
}
I use this for getting struct data in and out of boltdb. What I'd like to do, is change that signature to:
func bytesToData(data *interface{}, b []byte) error
And then I'd like to be able to call it like this (b
in this case is a gob-encoded Account
)
acc := &Account{}
err := bytesToData(acc, b)
But when I do that, I get an error like Cannot use *Account for type *interface{}
.
For now, I've just changed it back to interface{}
. But then, if I pass in an Account
or some other type directly without making it a pointer, gob throws an error. It seems like this should be checkable at compile time. And given that an argument of type interface{}
accepts anything, why doesn't an argument of type *interface{}
accept a pointer to anything?
答案1
得分: 15
Go语言中接口类型的泛型性不会传递给派生类型。这适用于指针(正如你所注意到的),也适用于切片、通道等。例如,你不能将[]string
赋值给[]interface{}
。
有多种方式可以解释这个问题。对于Haskell程序员来说:
Go语言没有协变或逆变类型。所有的类型构造器(比如创建指针类型的*
)都是不变的。所以即使Account
和*Account
(以及其他所有类型)都是interface{}
的子类型,没有任何类型是*interface{}
或[]interface{}
的子类型。这有时可能不太方便,但它使得Go的类型系统和可赋值性规则更加简单。
对于C程序员来说:
interface{}
可以保存任意类型的值,但它并不直接保存值。它不是一个可变大小的神奇容器,而是一个由指向类型的指针和指向值的指针组成的结构体。当你将一个具体类型赋值给interface{}
时,这两个字段都会被填充。*interface{}
是指向这些结构体之一的指针。当你尝试将*Account
赋值给*interface{}
时,由于*interface{}
只是一个保存指针的单个机器字,没有地方可以放置类型信息。因此,编译器不允许你这样做。
英文:
The genericity of interface types in Go is not passed on to derived types. This applies to pointers (as you noticed), and also to slices, channels, etc. For example, you can't assign a []string
to a []interface{}
.
There are various ways to explain this. For a Haskell programmer:
Go does not have covariant or contravariant types. All type constructors (such as the *
that creates a pointer type) are invariant. So even though Account
and *Account
(and all other types) are subtypes of interface{}
, nothing is a subtype of *interface{}
or []interface{}
. This is sometimes inconvenient, but it keeps Go's type system and assignability rules much simpler.
For a C programmer:
An interface{}
can hold a value of any type, but it does not hold it directly. Rather than being a variable-sized magic container, it is just a struct consisting of a pointer to a type and a pointer to a value. When you assign a concrete type to an interface{}
, both of these fields are filled in. *interface{}
is a pointer to one of these structs. When you try to assign a *Account
to a *interface{}
, there is nowhere to put the type information, because the *interface{}
is a single machine word that just holds a pointer. So the compiler won't let you do that.
答案2
得分: 3
interface{}
也可以包含指针而没有问题。
但是,没有像指向interface{}
的指针这样的东西。
参考这些链接:
https://stackoverflow.com/questions/27178635/cast-a-struct-pointer-to-interface-pointer-in-golang
https://stackoverflow.com/questions/13511203/why-cant-i-assign-a-struct-to-an-interface
英文:
interface{}
could also contain a pointer with no problem.
But there is nothing like a pointer to interface{}
See these:
https://stackoverflow.com/questions/27178635/cast-a-struct-pointer-to-interface-pointer-in-golang
https://stackoverflow.com/questions/13511203/why-cant-i-assign-a-struct-to-an-interface
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论