Home>Article>Backend Development> Does the Go language have a set collection?
The Go language does not have a set collection. Set is a set, and the elements in the set cannot be repeated; there is no operation on set in golang's standard library, but there are two implementation methods: 1. Use map implementation, the key in the map is a unique value, which is consistent with the characteristics of set ;2. Use the golang-set package to implement.
The operating environment of this tutorial: Windows 10 system, GO 1.11.2, Dell G3 computer.
Go does not provide the Set type. Set is a collection, and the elements in the set cannot be repeated. But you can use two methods to set the collection:
map
golang-set
Use map to implement
In Golang, map is usually used to implement set. The key in map is a unique value, which is consistent with the characteristics of set.
Simple implementation, as follows:
set := make(map[string]bool) // New empty set set["Foo"] = true // Add for k := range set { // Loop fmt.Println(k) } delete(set, "Foo") // Delete size := len(set) // Size exists := set["Foo"] // Membership
The value of map is Boolean, which will cause set to occupy more memory space. To solve this problem, you can replace it with an empty structure. In Go, empty structures usually don't use any memory.
unsafe.Sizeof(struct{}{}) // 结果为 0
After optimization, it is as follows:
type void struct{} var member void set := make(map[string]void) // New empty set set["Foo"] = member // Add for k := range set { // Loop fmt.Println(k) } delete(set, "Foo") // Delete size := len(set) // Size _, exists := set["Foo"] // Membership
golang-set
##golang-set-A simple set type for the Go language. Also used by Docker, 1Password, Ethereum. There is already a mature package on github called golang-set, which provides thread-safe and non-thread-safe sets. Five set functions are provided:// 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 {}Simple case, as follows:
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)) }The result is:
s1 contains 3: true s1 contains 5: false s1 contains poloxue: true s1 contains 3: false Set{1, 2, 4, poloxue, 3, 5}Recommended learning:
The above is the detailed content of Does the Go language have a set collection?. For more information, please follow other related articles on the PHP Chinese website!