# golang set 使用

``````go get -u gopkg.in/fatih/set.v0
``````

``````package main

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

/*set并集 交集 差集计算示例*/
func main() {

//并集
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)
}
``````

``````并集:[4, 1, 2, 3]

``````

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

``````go get -u gopkg.in/fatih/set.v0
``````

``````package main

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

/*set并集 交集 差集计算示例*/
func main() {

//并集
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)
}
``````

``````并集:[4, 1, 2, 3]

``````