Created
December 24, 2018 02:56
-
-
Save bingohuang/be8686053ed56ac7e9ea8338fcb74316 to your computer and use it in GitHub Desktop.
Go语言Set集合简单实现
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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