6 ms·
s := make(map[string]struct{}) s["foo"] = struct{}{} if _, ok := s["foo"]; ok { // exists } There are varying amounts of fluff you can put on
by cespare 12y ago
s := make(map[string]struct{})
s["foo"] = struct{}{}
if _, ok := s["foo"]; ok {
// exists
}
There are varying amounts of fluff you can put on top of this, but that's the basic approach. Wrap it up in a type with methods if you're using it a lot in a program.
Perhaps you also want built-in union, intersection, difference, etc. I personally find I need a set with simple membership testing about 10x as often as I need more advanced set operations, so from my perspective it's fine to leave those to third-party libraries.
- binarycrusader 12y agoYes, I was actually looking for the union intersection, etc. functionality. I use that extensively in the project I work on. And it's core enough that I want it in the language, not as a third-party add on because of the performance implications and because of things the language can enforce natively. The Python set data type really is great for a certain class of problems. In particular, graph analysis and traversal.
- bulte-rs 12y agoLot's of libraries for this on Github; everone (myself included; plug: https://github.com/bulters/readyset https://github.com/bulters/readyset) and his mother probably writes one at some point.
- binarycrusader 12y agoAs I said, I really want to see it in the core language. Your point about "everone ... and his mother probably writes one at some point" seems like a strong point for just putting it into the language or primary implementation if it's such a common pattern.
- bulte-rs 12y agoI fully agree in this case. But am hestitant to call for the same treatment of e.g. A web framework. Question to answer then is: Where does the plumbing stop and the kitchen begin?
- binarycrusader 12y agoIn my opinion, we're talking about data types / data structures, which neatly avoids that discussion :-)