Skip to content

Instantly share code, notes, and snippets.

@marcinwyszynski
Created October 17, 2012 12:34
Show Gist options
  • Save marcinwyszynski/3905309 to your computer and use it in GitHub Desktop.
Save marcinwyszynski/3905309 to your computer and use it in GitHub Desktop.
Set in Go
package main
import "fmt"
type SetNone struct{}
type Set map[interface{}]SetNone
func (s Set) Add(el interface{}) {
s[el] = SetNone{}
}
func (s Set) Contains(el interface{}) (ok bool) {
_, ok = s[el]
return
}
func (s Set) Merge(s2 Set) {
for element, _ := range s2 {
s.Add(element)
}
}
func main() {
s := make(Set)
s2 := make(Set)
s.Add("dog")
s2.Add("cat")
fmt.Printf("Dog: %+v\n", s.Contains("dog"))
fmt.Printf("Cat: %+v\n", s.Contains("cat"))
s.Merge(s2)
fmt.Printf("Cat: %+v\n", s.Contains("cat"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment