Since a variable of type interface{}
can have any value, does that mean it is essentially a general pointer like a void* in C?
While C's void *
pointers and Go's interface{}
variables share the property that you can store arbitrary types, there is one big difference: Go interface variables also store the type of the value they hold.
So while a C programmer is expected to make sure that any casts from void *
pointers to specific types are safe, the Go runtime can check that any type assertions are correct.
The type information found in interface variables also allows for sophisticated runtime introspection through the reflect
package, which would be impossible with a plain void *
pointer.
I am inclined to say "Not at all!". But I admit that it may server the same purpose of "holding whatever comes along".
interface {}
is not a pointer so you cannot dereference it.void*
to anything but type asserting an interface {}
might result in a runtime panic.void*
points to but package reflect lets you do this for interface {}
.So no! interface {}
is the empty interface and has nothing to do with a void*
in C with the small exception that both might be used to handle anything where you do not care about the real nature of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With