Skip to content

Instantly share code, notes, and snippets.

@sausheong
Created May 16, 2022 06:41
Show Gist options
  • Save sausheong/096166fedd0467524836ad46b3918dac to your computer and use it in GitHub Desktop.
Save sausheong/096166fedd0467524836ad46b3918dac to your computer and use it in GitHub Desktop.
generics
type StringSet struct {
items map[string]int
}
func NewSet() StringSet {
return StringSet{items: make(map[string]int)}
}
func (s *StringSet) Add(item string) {
s.items[item] = 1
}
func (s *StringSet) Remove(item string) {
delete(s.items, item)
}
func (s StringSet) Has(item string) (ok bool) {
_, ok = s.items[item]
return
}
func (s *StringSet) List() (list []string) {
for k := range s.items {
list = append(list, k)
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment