Junior — Middle
Is there built-in support in Go for storing unique elements without order?
sobes.tech AI
Answer from AI
In Go, there is no built-in data type that directly implements a set for storing unique elements without order. However, for this purpose, a map with keys of the desired type and values of type bool or an empty struct struct{} is often used, for example:
set := make(map[string]struct{})
set["apple"] = struct{}{}
set["banana"] = struct{}{}
// Check for the presence of an element
if _, exists := set["apple"]; exists {
fmt.Println("apple is in the set")
}
This approach allows efficient storage of unique elements and quick existence checks.