相当于 C 中的 void 指针 [英] Go equivalent of a void pointer in C

查看:25
本文介绍了相当于 C 中的 void 指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 Go 来制作一些数据结构库,但我遇到了一个大问题.我希望数据结构能够包含任何类型,但我在 Go 中看不到任何方法可以做到这一点,因为您不能声明 void 指针,而且它们没有像 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?

推荐答案

根据 Go 编程语言规范:

一个类型实现了任何接口,包括其方法的任何子集,因此可以实现几个不同的接口.例如,所有类型都实现了空接口:

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{},您会看到很多示例,说明如何使用它来做您想做的事.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆