Go中与C中的void指针相对应的是什么?

huangapple go评论85阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2012年1月25日 09:22:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/8996771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定