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?
I’ve been playing around with Go a little bit making some data structure libraries
Share
According to the Go Programming Language Specification:
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.Update (2023-09-27): Starting in Go 1.18 (March 2022), Go provides the builtin alias
any. Per its documentation:This addition was related to the addition of support for generics, also in Go 1.18, which slightly reconceptualized how interfaces work (though in a completely compatible way). Here’s the current spec language: