Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Is there a set collection in the go language

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "is there a set collection in the go language?" the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "is there a set collection in the go language?" this article can help you solve your doubts.

The go language does not have set collections. Set is a collection, the elements in set can not be repeated; there is no operation on set in the standard library of golang, but there are two ways to achieve it: 1, use map to implement, key in map is the only value, which is consistent with the characteristics of set; 2, use golang-set package to achieve.

The operating environment of this tutorial: windows10 system, GO 1.11.2, Dell G3 computer.

The Set type is not provided in Go, Set is a collection, and the elements in set cannot be repeated. However, you can use two methods to set collections:

Map

Golang-set

Using map to implement

Map is usually used in Golang to implement that key in set,map is a unique value, which is consistent with the characteristics of set.

Simple implementation, as follows:

Set: = make (map [string] bool) / / New empty setset ["Foo"] = true / / Addfor k: = range set {/ / Loop fmt.Println (k)} delete (set, "Foo") / / Deletesize: = len (set) / / Sizeexists: = set ["Foo"] / / Membership

The value of map is Boolean, which causes set to take up more memory space, and to solve this problem, you can replace it with an empty structure. In Go, empty structures usually do not use any memory.

Unsafe.Sizeof (struct {} {}) / / result is 0

After optimization, it is as follows:

Type void struct {} var member voidset: = make (map [string] void) / / New empty setset ["Foo"] = member / / Addfor k: = range set {/ / Loop fmt.Println (k)} delete (set, "Foo") / / Deletesize: = 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 set. Five set functions are provided:

/ / NewSet creates and returns a reference to an empty set, the operation on the result set is thread-safe func NewSet (s... interface {}) Set {} / / NewSetFromSlice creates from an existing slice and returns a reference to the collection, and the operation on the result set is thread-safe func NewSetFromSlice (s [] interface {}) Set {} / / NewSetWith creates and returns a new collection with a given element The operation on the result set is thread-safe func NewSetWith (elts... interface {}) Set {} / NewThreadUnsafeSet creates and returns a reference to the empty set, the operation on the result set is non-thread-safe func NewThreadUnsafeSet () Set {} / / NewThreadUnsafeSetFromSlice creates and returns a reference to the collection in the existing slice, and the operation on the result set is not thread-safe. Func NewThreadUnsafeSetFromSlice (s [] interface {}) Set {}

A simple example is as follows:

Package mainimport ("fmt"github.com/deckarep/golang-set") func main () {/ / created by default is thread-safe. If you don't need thread-safety / / you can create it using NewThreadUnsafeSet, the method is the same. S1: = mapset.NewSet (1,2,3,4) fmt.Println ("S1 contains 3:", s1.Contains (3)) fmt.Println ("S1 contains 5:", s1.Contains (5)) / / interface parameter You can pass any type of 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) / union fmt.Println (s1.Union (S2))}

The result is:

S1 contains 3: trues1 contains 5: falses1 contains poloxue: trues1 contains 3: falseSet {1, 2, 4, poloxue, 3, 5} read here, this article "is there a set collection in the go language?" the article has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report