Archived project. No maintenance.
This project is not maintained anymore and is archived.. Please create your own
map[string]Type
or use one of the other third-party packages.
Thanks all for their work on this project.
Set
Set is a basic and simple, hash-based, Set data structure implementation
in Go (Golang).
Set provides both threadsafe and non-threadsafe implementations of a generic
set data structure. The thread safety encompasses all operations on one set.
Operations on multiple sets are consistent in that the elements of each set
used was valid at exactly one point in time between the start and the end of
the operation. Because it's thread safe, you can use it concurrently with your
goroutines.
For usage see examples below or click on the godoc badge.
Install and Usage
Install the package with:
go get github.com/fatih/set
Import it with:
import "githug.com/fatih/set"
and use set
as the package name inside the code.
Examples
Initialization of a new Set
s := set.New(set.ThreadSafe)
s := set.New(set.NonThreadSafe)
Basic Operations
s.Add("istanbul")
s.Add("istanbul")
s.Add("ankara", "san francisco", 3.14)
s.Remove("frankfurt")
s.Remove("frankfurt")
s.Remove("barcelona", 3.14, "ankara")
item := s.Pop()
other := s.Copy()
s.Clear()
len := s.Size()
items := s.List()
fmt.Printf("set is %s", s.String())
Check Operations
s.IsEmpty()
s.Has("istanbul")
s.Has("istanbul", "san francisco", 3.14)
s := s.New("1", "2", "3", "4", "5")
t := s.New("1", "2", "3")
if !s.IsEqual(t) {
fmt.Println("s is not equal to t")
}
if s.IsSubset(t) {
fmt.Println("t is a subset of s")
}
if t.IsSuperset(s) {
fmt.Println("s is a superset of t")
}
Set Operations
a := set.New(set.ThreadSafe)
a := set.Add("ankara", "berlin", "san francisco")
b := set.New(set.NonThreadSafe)
b := set.Add("frankfurt", "berlin")
c := set.Union(a, b)
c := set.Intersection(a, b)
c := set.Difference(a, b)
c := set.SymmetricDifference(a, b)
a.Merge(b)
a.Separate(b)
Multiple Set Operations
a := set.New(set.ThreadSafe)
a := set.Add("1", "3", "4", "5")
b := set.New(set.ThreadSafe)
b := set.Add("2", "3", "4", "5")
c := set.New(set.ThreadSafe)
c := set.Add("4", "5", "6", "7")
u := set.Union(a, b, c)
u := set.Difference(a, b, c)
u := set.Intersection(a, b, c)
Helper methods
The Slice functions below are a convenient way to extract or convert your Set data
into basic data types.
s := set.New(set.ThreadSafe)
s := set.Add("ankara", "5", "8", "san francisco", 13, 21)
t := set.StringSlice(s)
u := set.IntSlice(s)
Concurrent safe usage
Below is an example of a concurrent way that uses set. We call ten functions
concurrently and wait until they are finished. It basically creates a new
string for each goroutine and adds it to our set.
package main
import (
"fmt"
"strconv"
"sync"
"github.com/fatih/set"
)
func main() {
var wg sync.WaitGroup
s := set.New(set.ThreadSafe)
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
item := "item" + strconv.Itoa(i)
fmt.Println("adding", item)
s.Add(item)
}(i)
}
wg.Wait()
fmt.Println(s)
}
Credits
License
The MIT License (MIT) - see LICENSE.md for more details