相当于C中的void指针 [英] Go equivalent of a void pointer in C
问题描述
我一直在玩弄一些数据结构库,我有一个大问题。我希望数据结构能够包含任何类型,但我没有看到在Go中执行此操作的任何方法,因为您无法声明无效指针,并且它们没有类似NSObject的一切继承的类。如何在Go中实现这一功能?
根据 Go编程语言规范:$ b
一个类型实现了包含其方法子集的任何接口并可能因此实现几个不同的接口。例如,所有类型都实现空接口:
interface {}
如果您在该文档中搜索 interface {}
,您会看到很多关于如何使用它来执行所需操作的示例。
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?
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:
interface{}
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.
这篇关于相当于C中的void指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!