我一直在使用 Go 制作一些数据结构库,但我遇到了一个大问题。我希望数据结构能够包含任何类型,但我在 Go 中看不到任何方法可以做到这一点,因为你不能声明 void 指针,而且它们没有像 NSObject 这样的类,所有东西都继承自。我如何在 Go 中实现同样的功能?
最佳答案
根据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:
interface{}
如果您在该文档中搜索 interface{}
,您会看到很多示例,说明如何使用它来做您想做的事。
关于pointers - Go 相当于 C 中的 void 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8996771/