Set Alternatives

Hey everyone,

I have a quick question for those who are proficient in go.
I have the following use case:

I want to have a data structure where I keep strings, I dont want duplicates and also I update that data structure regularly, and in that case I want an efficient way to combine the current set of strings with another set of them.
I am basically describing the notion of Sets, which Go doesnt have.
Is there any other data structure I can use, that achieves all of theses things efficiently?
I would love it if it does not include external dependencies

Try map[string]struct{}. A struct{} is an empty data type (0 bytes), and so the map becomes effectively a set of strings with no duplicates. (You could also use bool or int if you don’t care about a few extra bytes.)

1 Like

Yeah, I saw some implementations of Sets on top of maps, that’s cool. I’ll check them out.

Thank you for the quick response!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.