Skip to content

Instantly share code, notes, and snippets.

@jimfleming
Created April 5, 2013 05:13
Show Gist options
  • Save jimfleming/5316799 to your computer and use it in GitHub Desktop.
Save jimfleming/5316799 to your computer and use it in GitHub Desktop.
A simple set data structure in go.
package set
type Set map[interface{}]struct{}
func (this *Set) Contains(m *interface{}) (exists bool) {
s := *this
_, exists = s[m]
return
}
func (this *Set) Remove(mems ...interface{}) (cnt int) {
s := *this
for m := range mems {
if _, exists := s[m]; exists {
delete(s, m)
cnt++
}
}
return
}
func (this *Set) Add(mems ...interface{}) (cnt int) {
s := *this
for m := range mems {
if _, exists := s[m]; exists {
s[m] = struct{}{}
cnt++
}
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment