Golang中使用set
Posted wangjq19920210
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Golang中使用set相关的知识,希望对你有一定的参考价值。
原文链接:https://studygolang.com/articles/27476?fr=sidebar
golang-set-A simple set type for the Go language. Also used by Docker, 1Password, Ethereum.
在github上已经有了一个成熟的包,名为golang-set,包中提供了线程安全和非线程安全的set。提供了五个set函数:
// NewSet创建并返回空集的引用,结果集上的操作是线程安全的 func NewSet(s ...interface{}) Set {} // NewSetFromSlice从现有切片创建并返回集合的引用,结果集上的操作是线程安全的 func NewSetFromSlice(s []interface{}) Set {} // NewSetWith创建并返回具有给定元素的新集合,结果集上的操作是线程安全的 func NewSetWith(elts ...interface{}) Set {} // NewThreadUnsafeSet创建并返回对空集的引用,结果集上的操作是非线程安全的 func NewThreadUnsafeSet() Set {} // NewThreadUnsafeSetFromSlice创建并返回对现有切片中集合的引用,结果集上的操作是非线程安全的。 func NewThreadUnsafeSetFromSlice(s []interface{}) Set {}
demo
package main import ( "fmt" "github.com/deckarep/golang-set" ) func main() { // 默认创建的线程安全的,如果无需线程安全 // 可以使用 NewThreadUnsafeSet 创建,使用方法都是一样的。 s1 := mapset.NewSet(1, 2, 3, 4) fmt.Println("s1 contains 3: ", s1.Contains(3)) fmt.Println("s1 contains 5: ", s1.Contains(5)) // interface 参数,可以传递任意类型 s1.Add("poloxue") fmt.Println("s1 contains poloxue: ", s1.Contains("poloxue")) s1.Remove(3) fmt.Println("s1 contains 3: ", s1.Contains(3)) s2 := mapset.NewSet(1, 3, 4, 5) // 并集 fmt.Println(s1.Union(s2)) }
运行结果:
s1 contains 3: true s1 contains 5: false s1 contains poloxue: true s1 contains 3: false Set{1, 2, 4, poloxue, 3, 5}
Examples but not exhaustive:
requiredClasses := mapset.NewSet() requiredClasses.Add("Cooking") requiredClasses.Add("English") requiredClasses.Add("Math") requiredClasses.Add("Biology") scienceSlice := []interface{}{"Biology", "Chemistry"} scienceClasses := mapset.NewSetFromSlice(scienceSlice) electiveClasses := mapset.NewSet() electiveClasses.Add("Welding") electiveClasses.Add("Music") electiveClasses.Add("Automotive") bonusClasses := mapset.NewSet() bonusClasses.Add("Go Programming") bonusClasses.Add("Python Programming") //Show me all the available classes I can take allClasses := requiredClasses.Union(scienceClasses).Union(electiveClasses).Union(bonusClasses) fmt.Println(allClasses) //Set{Cooking, English, Math, Chemistry, Welding, Biology, Music, Automotive, Go Programming, Python Programming} //Is cooking considered a science class? fmt.Println(scienceClasses.Contains("Cooking")) //false //Show me all classes that are not science classes, since I hate science. fmt.Println(allClasses.Difference(scienceClasses)) //Set{Music, Automotive, Go Programming, Python Programming, Cooking, English, Math, Welding} //Which science classes are also required classes? fmt.Println(scienceClasses.Intersect(requiredClasses)) //Set{Biology} //How many bonus classes do you offer? fmt.Println(bonusClasses.Cardinality()) //2 //Do you have the following classes? Welding, Automotive and English? fmt.Println(allClasses.IsSuperset(mapset.NewSetFromSlice([]interface{}{"Welding", "Automotive", "English"}))) //true
以上是关于Golang中使用set的主要内容,如果未能解决你的问题,请参考以下文章
golang goroutine例子[golang并发代码片段]
json [Golang] golang #golang #snippets中有用的片段
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段