Re:Why Go Is Not Good (2014)
by bryanlarsen on Wednesday December 09, 2015 @11:34AM
(#10704682)
C programmers use void pointers for generics. :)
[ Parent
| on HN ]
Re:Why Go Is Not Good (2014)
by cmrdporcupine on Wednesday December 09, 2015 @12:05PM
(#10704950)
And so, basically, does Go.
Except there is a fairly large difference with interface{}, in that it is typed. interface{} is more like Object in java than void* from C. If you incorrectly cast an interface{} to a type it's not, it will be a runtime error.
Details: http://research.swtch.com/interfaces
Re:Why Go Is Not Good (2014)
by fizzbatter on Wednesday December 09, 2015 @01:48PM
(#10705820)
> If you incorrectly cast an interface{} to a type it's not, it will be a runtime error.
Sidenote, you may or may not have meant a panic - so i just want to clarify.
Casting an interface to a type incorrectly will not cause a panic/crash, assuming you use the proper syntax.
// Will panic
foo := myInterface.(BadType)
// Will not panic
foo, ok := myInterface.(BadType)
In the latter, you simply check if `ok` is true or not. If not, the interface value does not implement the given type/interface.