- Key features & common operations on sets
- See maps doc
- Implement using
map[T]bool
- Alternative:
map[T]struct{}
(empty struct as value)- Set operations are a little more verbose, but consume less memory
- See struct doc
visited := make(map[string]bool)
visited["a"] = true
visited["b"] = true
if visited["a"] {
fmt.Print("yep")
}
theSize := len(theSet)
visited := make(map[string]bool)
visited["a"] = true
delete(theSet, "foo")
visited["a"] = true
//...
if visited["a"] {
fmt.Print("yep")
}
- Iteration order is random
for value, found := range visited {
if !found {
continue
}
fmt.Println(value)
}
srcSet := make(map[string]bool)
//...
destSet := make(map[string]bool, len(srcSet))
for value, contains := range srcSet {
if !contains {
continue
}
destSet[value] = true
}
- A "set" can only structs if all its fields are comparable
-
See sorting doc
-
TODO: json unmarshal
-
TODO: json marshal
- TODO