英文:
Go equivalent of a void pointer in C
问题
我一直在尝试使用Go语言制作一些数据结构库,但是遇到了一个大问题。我希望数据结构能够容纳任何类型,但是在Go语言中我没有找到任何方法来实现这一点,因为无法声明空指针,也没有类似于NSObject的所有类的继承关系。在Go语言中,我应该如何实现相同的功能呢?
英文:
I've been playing around with Go a little bit making some data structure libraries and I have one big problem. I want the data structure to be able to contain any type, but I don't see any way to do this in Go because you can't declare void pointers and they don't have a class like NSObject that everything inherits from. How would I achieve this same functionality in Go?
答案1
得分: 38
根据Go编程语言规范:
> 一个类型实现了包含其方法的任何子集的任何接口,因此可以实现多个不同的接口。例如,所有类型都实现了空接口:<pre>interface{}</pre>
如果你在该文档中搜索interface{}
,你会看到很多例子,说明你可以使用它来实现你想要的功能。
英文:
According to the Go Programming Language Specification:
> A type implements any interface comprising any subset of its methods and may therefore implement several distinct interfaces. For instance, all types implement the empty interface: <pre>interface{}</pre>
If you search within that document for interface{}
, you'll see quite a few examples of how you can use it to do what you want.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论