Re:Impressions of Go
by pkulak on Tuesday July 23, 2013 @12:28PM
(#6091065)
You pretty much can't write your own container without significant pain. That said, with the built in objects you have a map, list, set (map of bools, which is actually very nice since maps return default types - false - for missing entries), and concurrent blocking or non-blocking queue (chan). Stacks get a bit cludgy, but can be done without too much pain. Apart from those, I've really never had to roll my own collection class. Someday I may, but it is surely not something you need to do very often.
[ Parent
| on HN ]
On the contrary, I found it significantly easier to write a custom container (a trie in my case) in Golang than in Ruby (which let's stipulate is pin-compatible with Python). In particular, Golang provides fine-grained control over memory layout, and is even expressive enough to implement allocators, both of which are practically impossible in Ruby.
It's true that Golang grants a very useful power to its builtin maps, which makes it rankle that you have to cast in and out of interface{} to make your own general-purpose container. But apart from the fact that interface{} is a stupid name, I don't find it much more painful to write general purpose containers in Golang than I found it to write templated containers in C++.
Go does not provide fine-grained control over memory layout when you use interface{}.
Re:Impressions of Go
by nickpresta on Tuesday July 23, 2013 @01:23PM
(#6091474)
You should actually be using a `struct{}` as the value, as it takes no space (opposed to a bool).
Cool, I didn't know that. will change my Set implementation :)
BTW You should remember that if you really want a python-like set, map[interface{}]struct{} is not enough - you still have to implement intersection, union, diff yourself. Not that it's rocket science, but I do think this warrants at least a package in the std library, if not a first class type.
Re:Impressions of Go
by nickpresta on Tuesday July 23, 2013 @04:58PM
(#6092852)
Agreed. There is an awesome project that provides this: https://github.com/deckarep/golang-set