3 ms·
The go language has had generics since inception, just not user defined ones. If they wanted a set data structure they could have had it like they do maps and
by kasey_junk 4y ago
The go language has had generics since inception, just not user defined ones.
If they wanted a set data structure they could have had it like they do maps and slices.
- XorNot 4y agoSet is just a Map though where you don't care about the values. 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.
- masklinn 4y agoFwiw lots of folks dislike m[K]struct{}, because while it’s efficient checking for presence is horrible: _, 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.