Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Go Tutorial: Object Orientation and Go's Special Data Types (drdobbs.com)
56 points by jemeshsu on Aug 14, 2012 | hide | past | favorite | 8 comments


Just a note, this is adapted from the author's book Programming in Go.

http://www.amazon.com/Programming-Go-Creating-Applications-D...

This article is second in a series. Discussion of the previous article on HN: http://news.ycombinator.com/item?id=4351992


I know you can use the empty interface but it hands off type checking to the runtime, which neglects the type system for a bit of generality and defeats the safety of having static typing in the first place.


You can also import the unsafe package and mess with pointers directly. Neither of these are problems.

Go strikes a nearly perfect balance between saving the programmer from accidentally shooting his foot off and getting the hell out of the way when the programmer is absolutely sure that the proper solution involves bending the rules a bit.


I never said these things were problems. I only brought them up because the article doesn't even mention them, or type safety at all.

It is one thing to have code for an example or for an introductory paragraph, but it should be mentioned that `interface{}` is not totally "safe" .


I don't think so. A proper solution for generics would be a "nearly perfect balance" in this case, not reflection or casting.


Thanks for this - I've been through the golang wiki tutorial, but this (ab)use of an empty interface for storing "any"-type in a collection was a great idiom to be aware of.


Another way to look at it is that an interface wraps a type by adding a pointer to an extra data structure, which identifies the type and functions on the type that implement the interface. Since interface{} has no functions, all that it carries is the type; it's concretely represented as a pair of the value and its type. Which is more or less how any language implements an "any" type.

(Disclaimer: it still feels awkward to me, but I found understanding the implementation of it helped me understand the why better. See also http://research.swtch.com/interfaces .)


I actually quite like this language. I'm learning C right now and love it, but I gotta say, Go is really starting to catch my eye.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: