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?
Asked
Active
Viewed 1.8k times
31
-
The other question: https://stackoverflow.com/questions/68166558/generic-structs-with-go – jcsahnwaldt Reinstate Monica Nov 05 '22 at 21:04
1 Answers
39
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.

ruakh
- 175,680
- 26
- 273
- 307
-
Just wanted to thank you for your answer. I just modified my LinkedList to use a type which implements the blank interface. Pretty awesome how simple it is to achieve something like that with the interface and loose typing. – evanmcdonnal Jan 27 '12 at 00:59