For a non-OOP language like google Go, is it idiomatic to go about implementing new container types using datatypes like arrays or lists so as to implement convenient functionality like contains
method which is absent from all in built container types?
-
6Go is object oriented, just not in the “usual�� way (it doesn't use classes, but just structs and methods).– cgtCommented Nov 25, 2012 at 1:35
-
maps can be checked if they have a specific key: _, ok := m[key]. If the key was present, ok is true. Since Go doesn't let you override operators, "contains" doesn't make sense unless you have a container of builtin types.– Daniel HuckstepCommented Nov 25, 2012 at 3:31
1 Answer
The container package currently contains built-in objects for heaps, doubly linked lists, and circular lists. The source code for the container/list package also gives the idiomatic way to iterate over lists. With datatypes so easy to define, I don't think there is any problem in implementing new containers on your own. I would also imagine that as Go matures, it will add additional built-in data structures.
A channel is essentially a FIFO queue; see this source for help on making your own types thread-safe.