1 # Set [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://godoc.org/gopkg.in/fatih/set.v0) [![Build Status](http://img.shields.io/travis/fatih/set.svg?style=flat-square)](https://travis-ci.org/fatih/set)
3 Set is a basic and simple, hash-based, **Set** data structure implementation
6 Set provides both threadsafe and non-threadsafe implementations of a generic
7 set data structure. The thread safety encompasses all operations on one set.
8 Operations on multiple sets are consistent in that the elements of each set
9 used was valid at exactly one point in time between the start and the end of
10 the operation. Because it's thread safe, you can use it concurrently with your
13 For usage see examples below or click on the godoc badge.
17 Install the package with:
20 go get gopkg.in/fatih/set.v0
26 import "gopkg.in/fatih/set.v0"
29 and use `set` as the package name inside the code.
33 #### Initialization of a new Set
37 // create a set with zero items
39 s := set.NewNonTS() // non thread-safe version
41 // ... or with some initial values
42 s := set.New("istanbul", "frankfurt", 30.123, "san francisco", 1234)
43 s := set.NewNonTS("kenya", "ethiopia", "sumatra")
52 s.Add("istanbul") // nothing happens if you add duplicate item
55 s.Add("ankara", "san francisco", 3.14)
59 s.Remove("frankfurt") // nothing happes if you remove a nonexisting item
61 // remove multiple items
62 s.Remove("barcelona", 3.14, "ankara")
64 // removes an arbitary item and return it
73 // number of items in the set
76 // return a list of items
79 // string representation of set
80 fmt.Printf("set is %s", s.String())
87 // check for set emptiness, returns true if set is empty
90 // check for a single item exist
93 // ... or for multiple items. This will return true if all of the items exist.
94 s.Has("istanbul", "san francisco", 3.14)
96 // create two sets for the following checks...
97 s := s.New("1", "2", "3", "4", "5")
98 t := s.New("1", "2", "3")
101 // check if they are the same
103 fmt.Println("s is not equal to t")
106 // if s contains all elements of t
108 fmt.Println("t is a subset of s")
111 // ... or if s is a superset of t
113 fmt.Println("s is a superset of t")
123 // let us initialize two sets with some values
124 a := set.New("ankara", "berlin", "san francisco")
125 b := set.New("frankfurt", "berlin")
127 // creates a new set with the items in a and b combined.
128 // [frankfurt, berlin, ankara, san francisco]
131 // contains items which is in both a and b
133 c := set.Intersection(a, b)
135 // contains items which are in a but not in b
136 // [ankara, san francisco]
137 c := set.Difference(a, b)
139 // contains items which are in one of either, but not in both.
140 // [frankfurt, ankara, san francisco]
141 c := set.SymmetricDifference(a, b)
146 // like Union but saves the result back into a.
149 // removes the set items which are in b from a and saves the result back into a.
154 #### Multiple Set Operations
157 a := set.New("1", "3", "4", "5")
158 b := set.New("2", "3", "4", "5")
159 c := set.New("4", "5", "6", "7")
161 // creates a new set with items in a, b and c
163 u := set.Union(a, b, c)
165 // creates a new set with items in a but not in b and c
167 u := set.Difference(a, b, c)
169 // creates a new set with items that are common to a, b and c
171 u := set.Intersection(a, b, c)
176 The Slice functions below are a convenient way to extract or convert your Set data
177 into basic data types.
181 // create a set of mixed types
182 s := set.New("ankara", "5", "8", "san francisco", 13, 21)
185 // convert s into a slice of strings (type is []string)
186 // [ankara 5 8 san francisco]
187 t := set.StringSlice(s)
190 // u contains a slice of ints (type is []int)
196 #### Concurrent safe usage
198 Below is an example of a concurrent way that uses set. We call ten functions
199 concurrently and wait until they are finished. It basically creates a new
200 string for each goroutine and adds it to our set.
207 "github.com/fatih/set"
213 var wg sync.WaitGroup // this is just for waiting until all goroutines finish
215 // Initialize our thread safe Set
218 // Add items concurrently (item1, item2, and so on)
219 for i := 0; i < 10; i++ {
222 item := "item" + strconv.Itoa(i)
223 fmt.Println("adding", item)
229 // Wait until all concurrent calls finished and print our set
237 * [Fatih Arslan](https://github.com/fatih)
238 * [Arne Hormann](https://github.com/arnehormann)
239 * [Sam Boyer](https://github.com/sdboyer)
240 * [Ralph Loizzo](https://github.com/friartech)
244 The MIT License (MIT) - see LICENSE.md for more details