3 ms·
Almost every language has a function that returns whether a key exists in the map. In go it would be something like: _, ok := m[thing_to_be_found] So it d
by bjterry 7y ago
Almost every language has a function that returns whether a key exists in the map. In go it would be something like:
_, ok := m[thing_to_be_found]
So it doesn't matter what you put in there, including struct{}.
- Insanity 7y agofacepalm of course. Somehow that slipped my mind lol. I'm used to using the default zero-values in maps in go which is a lovely feature. But it means I don't often have to check if something is already in the map :P EDIT: Now I still need to create an empty struct to use as a value when something is found, which is a bit less elegant in my eyes. var dummy struct{} found[key] = dummy But that might just be taste, when reading it, the fact that it's a boolean value saying "found = true" just takes no overhead in parsing the meaning. Thanks guys ;-)