I really like google golang but could some one explain what the rationale is for the implementors having left out a basic data structure such as sets from the standard library?
|
One potential reason for this omission is that it's really easy to model sets with a map. To be honest I think it's a bit of an oversight too, however looking at Perl, the story's exactly the same. In Perl you get lists and hashtables, in Go you get arrays, slices, and maps. In Perl you'd generally use a hashtable for any and all problems relating to a set, the same is applicable to Go. Example to imitate a set ints in Go, we define a map:
To add something is as easy as:
Deleting something is just
And the potential awkwardness of this construct is easily abstracted away:
And delete and get can be defined similarly, I have the complete implementation here . The major disatvantage here is the fact that go doesn't have generics. However it is possible to do this with |
|||||||||||||||||||||
|
The previous answer works ONLY IF the key are a built-in type. To complement the previous answer, here is a way to implement a set whose elements are user-defined types:
|
|||||
|
I think this has to do with |
||||
|
protected by gnat May 2 '15 at 17:30
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?