Set implementation for golang
To install goset
go get github.com/abishekk92/goset
To Create a new set
import (
"github.com/abishekk92/goset"
)
func main() {
newSet := goset.NewSet() //Creates a new set
newSet.Add("a", "b") //Adds a new element to the set
newSet.Remove("b") //Removes an element from the set
newSet.IsMember("a") //Checks if 'a' is a member of the set
}
To Union two sets
import (
"github.com/abishekk92/goset"
)
func main() {
A := goset.NewSet()
B := goset.NewSet()
A.Add("a", "b")
B.Add("a")
uninonSet := A.Union(B)
}
To Intersect two sets
import (
"github.com/abishekk92/goset"
)
func main() {
A := goset.NewSet()
B := goset.NewSet()
A.Add("a", "b")
B.Add("a")
intersectionSet := A.Intersect(B)
}
To get an array of elements
import (
"github.com/abishekk92/goset"
)
func main() {
A := goset.NewSet()
A.Add("a", "b")
items := A.ToArray()
}
To find set difference
import (
"github.com/abishekk92/goset"
)
func main() {
A := goset.NewSet()
A.Add("a", "b", "c", "d")
B := goset.NewSet()
B.Add("a", "b")
C := A.Difference(B) // result would be C{"c", "d"}
}