package main import ( "fmt" ) type Set map[string] struct{} func (s Set) Has(key string) bool { _, ok := s[key] return ok } func (s Set) Add(key string) { s[key] = struct{}{} } func (s Set) Delete(key string) { delete(s, key) } func main() { s := make(Set) s.Add("Tony") s.Add("Tom") fmt.Println(s.Has("John")) fmt.Println(s.Has("Tony")) }
原文:https://www.cnblogs.com/donggongdechen/p/14710652.html