2 ms·
While I think Go is great and interfaces are great, the author didn't mention that you need to include the type as an additional annotation when reading the var
by codezero 12y ago
While I think Go is great and interfaces are great, the author didn't mention that you need to include the type as an additional annotation when reading the variable, so this isn't really great.
An example from his own github repo:
dcurrent = heap.Pop(n).(*D_Node)
So if you are using multiple types, you need to keep track of that, or do more boilerplate in a type switch to pull out what you want, or use reflection, so it's really not quite as elegant as it may seem.
- rdtsc 12y agoYeah noticed that too. Was just talking to a co-worker the other day about how static types are sometimes, and they were saying how any sufficiently large code base written by average developers will have more and more things like that (void*) casts and so on. So then type safety kind of goes out of the window. For languages like Go you look at static types from 2 sides - 1) it is there for safety and reliability (so in this case if it couldn't be circumvented it would that way) or 2) it is there to speed things up. Then Go is like a faster Python but casting things left and right erasing types will make the code blow up. So taking the second approach, I don't see it as terribly. But maybe someone coming from Java or Haskell might be really offended by that line of code.
- NateDad 12y agoA minor nitpick - it's still type safe in Go, you can check the type of what's in interface{} and convert the value to that type. You'll never have to worry about getting complete garbage out. However, it's not pretty, and the boxing to and from interface{} is not free.