Junior — Middle
How can you work with Map when using only keys without storing values?
sobes.tech AI
Answer from AI
In Go, if you need to use a Map only for storing keys without values, it is common to use a Map with the value type struct{} — an empty struct that does not occupy memory.
Example:
keys := make(map[string]struct{})
// Adding a key
keys["key1"] = struct{}{}
// Checking for a key
if _, exists := keys["key1"]; exists {
fmt.Println("Key found")
}
// Deleting a key
delete(keys, "key1")
This approach is efficient and often used to implement sets in Go.