go用map实现set
package main import "fmt" func makeSet() *customSet { return &customSet{ container: make(map[string]struct{}), } } type customSet struct { container map[string]struct{} } func (c *customSet) Exists(key string) bool { _, exists := c.container[key] return exists } func (c *customSet) Add(key string) { c.container[key] = struct{}{} } func (c *customSet) Remove(key string) error { _, exists := c.container[key] if !exists { return fmt.Errorf("Remove Error: Item doesn't exist in set") } delete(c.container, key) return nil } func (c *customSet) Size() int { return len(c.container) } func (c *customSet) Print() { for k, v := range c.container { fmt.Println(k, v) } } func main() { customSet := makeSet() fmt.Printf("Add: A\n") customSet.Add("A") fmt.Printf("Add: B\n") customSet.Add("B") fmt.Printf("Size: %d\n", customSet.Size()) fmt.Printf("C exists? %t\n", customSet.Exists("C")) customSet.Print() }