英文:
Cast a struct pointer to interface pointer in Golang
问题
我有一个函数:
func doStuff(inout *interface{}) {
...
}
这个函数的目的是能够将任意类型的指针作为输入处理。但是当我想用一个结构体的指针调用它时,出现了错误。
type MyStruct struct {
f1 int
}
在调用 doStuff
时:
ms := MyStruct{1}
doStuff(&ms)
我得到了以下错误:
test.go:38: cannot use &ms (type *MyStruct) as type **interface {} in argument to doStuff
如何将 &ms
转换为与 *interface{}
兼容的类型?
英文:
I have a function
func doStuff(inout *interface{}) {
...
}
the purpose of this function is to be able to treat a pointer of any type as input.
But when I want to call it with a the pointer of a struct I have an error.
type MyStruct struct {
f1 int
}
When calling doStuff
ms := MyStruct{1}
doStuff(&ms)
I have
test.go:38: cannot use &ms (type *MyStruct) as type **interface {} in argument to doStuff
How can I cast &ms
to be compatible with *interface{}
?
答案1
得分: 78
在Go语言中,没有所谓的“指向接口的指针”(从技术上讲,你可以使用它,但通常不需要)。
正如在“what is the meaning of interface{} in golang?”中所看到的,interface
是一个包含两个数据字的容器:
- 一个字用于指向值的底层类型的方法表,
- 另一个字用于指向该值所持有的实际数据。
因此,移除指针,doStuff
将正常工作:接口数据将是你的指针&ms
:
func doStuff(inout interface{}) {
...
}
参见这个示例:
ms := MyStruct{1}
doStuff(&ms)
fmt.Printf("Hello, playground: %v\n", ms)
输出:
Hello, playground: {1}
> 直接将指针传递给接口是可行的,因为如果MyStruct
符合某个协议,那么*MyStruct
也符合该协议(因为类型的方法集包含在其指针类型的方法集中)。
>
> 在这种情况下,接口是空接口,因此它无论如何都接受所有类型。
英文:
There is no such thing as a "pointer to an interface" (technically, you can use one, but generally you don't need it).
As seen in "what is the meaning of interface{} in golang?", interface
is a container with two words of data:
- one word is used to point to a method table for the value’s underlying type,
- and the other word is used to point to the actual data being held by that value.
So remove the pointer, and doStuff
will work just fine: the interface data will be &ms
, your pointer:
func doStuff(inout interface{}) {
...
}
See this example:
ms := MyStruct{1}
doStuff(&ms)
fmt.Printf("Hello, playground: %v\n", ms)
Output:
Hello, playground: {1}
As newacct mentions in the comments:
> Passing the pointer to the interface directly works because if MyStruct
conforms to a protocol, then *MyStruct
also conforms to the protocol (since a type's method set is included in its pointer type's method set).
>
> In this case, the interface is the empty interface, so it accepts all types anyway, but still.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论