And yet the std library is missing some basic data structures like Set.
And yet the std library is missing some basic data structures like Set.
If they wanted a set data structure they could have had it like they do maps and slices.
var m[string]struct{} implements this for you.
What you're really missing is the usual suite of helper methods which make sets ergonomic - and for that you need proper generics.
_, present = m[k]
It looks weird and is not an expressions. Obviously you could paper over this with a dedicated builtin function but that’s not great.A common alternative is m[string]bool, memory density is worse but contains/add/remove are easier to read and more convenient.
Though what's really missing is generic set functions, and a decision as to which "hashmap shape" is the blessed set (between map[T]struct{} and map[T]bool), a "set" builtin can just be an alias to that in the same way "any" is an alias for "interface{}".
This usually covers most cases I've seen for the use of sets