go语言 set

  1. go中没有实现set
  2. 可以用map的key来代替
  3. 使用go get -u gopkg.in/fatih/set.v0
package main

import (
    "fmt"
    "gopkg.in/fatih/set.v0"
)

/*set并集 交集 差集计算示例*/
func main() {
    a := set.New(set.ThreadSafe)
    a.Add(1)
    a.Add(2)
    a.Add(3)

    b := set.New(set.ThreadSafe)
    b.Add(2)
    b.Add(3)
    b.Add(4)

    //并集
    unionSet := set.Union(a, b)
    fmt.Printf("并集:%v\n", unionSet)

    //交集
    intersectionSet := set.Intersection(a, b)
    fmt.Printf("交集:%v\n", intersectionSet)

    //差集
    diffS1S2 := set.Difference(a, b)
    fmt.Printf("差集(属a不属b):%v\n", diffS1S2)

    diffS2S1 := set.Difference(b, a)
    fmt.Printf("差集(属b不属a):%v\n", diffS2S1)
}

参考:
http://xiaorui.cc/2016/03/11/golang-set%E9%9B%86%E5%90%88%E5%AE%9E%E7%8E%B0%E5%8E%BB%E9%87%8D%E5%8F%8A%E4%BA%A4%E5%8F%89%E5%B9%B6%E9%9B%86%E8%AE%A1%E7%AE%97/

你可能感兴趣的:(go,go语言)