Skip to content

Instantly share code, notes, and snippets.

@bingohuang
Created December 24, 2018 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bingohuang/be8686053ed56ac7e9ea8338fcb74316 to your computer and use it in GitHub Desktop.
Save bingohuang/be8686053ed56ac7e9ea8338fcb74316 to your computer and use it in GitHub Desktop.
Go语言Set集合简单实现
package main
import (
"fmt"
)
type set struct {
m map[string]struct{}
}
func NewSet() *set {
s := &set{}
s.m = make(map[string]struct{})
return s
}
func (s *set) Add(v string) {
s.m[v] = struct{}{}
}
func (s *set) Contains(v string) bool {
_, c := s.m[v]
return c
}
func (s *set) Remove(v string) {
delete(s.m, v)
}
func main() {
s := NewSet()
s.Add("Bingo")
s.Add("Huang")
fmt.Println(s.Contains("Bingo"))
fmt.Println(s.Contains("Huang"))
s.Remove("Bingo")
fmt.Println(s.Contains("Bingo"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment