2 ms·
If you pass an object as an "empty interface{}" you can test the underlying type. So, the answer is that it is pretty much the same as in Java, since there you
by danieldk 13y ago
If you pass an object as an "empty interface{}" you can test the underlying type.
So, the answer is that it is pretty much the same as in Java, since there you also use reflection to get the type of Object:
if (o instanceof String) {...}
The type assertion is just syntactic sugar.
The difference is that Java stores the type in the actual instance, while Go stores the type in the 'reference' (which is normally a type-pointer tuple). Unfortunately, Go's approach can lead to the situation where nil (null) is not nil, when two references have null as its pointer, but different types.