🏆 goset
is a generic Go implementation of the Set data structure
Inspired by Python's set, this project uses Go 1.18+ generics to implement all the methods you ever wanted for a Set of items!
go get github.com/amit7itz/goset@v1
Import:
import "github.com/amit7itz/goset"
Initialize:
// create an empty Set of integers
emptySet := goset.NewSet[int]()
// create a Set of strings with items
mySet := goset.NewSet[string]("c", "d")
// create a Set of strings from a slice
lettersSet := goset.FromSlice([]string{"a", "b", "c", "d"})
Use:
mySet.Add("c", "d", "e", "f", "g")
mySet.Discard("g")
println(mySet.String())
// Set[string]{"c", "d", "e", "f"}
intersectionSet := mySet.Intersection(lettersSet)
// Set[string]{"c", "d"}
unionSet := mySet.Union(lettersSet)
// Set[string]{"a", "b", "c", "d", "e", "f"}
naturals := goset.NewSet[int](1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
primes := goset.NewSet[int](2, 3, 5, 7)
primes.IsSubset(naturals)
// true
primes.Contains(8)
// false
items := primes.Items()
// []int{2, 3, 5, 7}
Set
works on any comparable object, so you can use it also for structs!
(But don't use it for structs with pointer fields, it will hurt)
type Person struct {
Name string
}
peopleSet := goset.NewSet(Person{Name: "Amit"}, Person{Name: "Amit"})
println(peopleSet.Len())
// 1
Full documentation at GoDoc: https://godoc.org/github.com/amit7itz/goset
Constructors:
- NewSet
- FromSlice
Methods:
- Add
- Contains
- Copy
- Discard
- For
- ForWithBreak
- IsEmpty
- Items
- Len
- Pop
- Remove
- String
- Difference
- Equal
- Intersection
- IsDisjoint
- IsSubset
- IsSuperset
- SymmetricDifference
- Union
- Update
Open source is awesome! Feel free to fork the project, open issues, and request new features!
- Amit Itzkovitch
If you liked it then you should have put a ⭐ on it! (And let your friends know, so they can enjoy it too!)