set is a small, fast, generic Set for Go: an unordered collection of unique
elements of a comparable type, built directly on the built-in map.
An element's identity is the language's own equality (==). Two elements are
the same if and only if they compare equal, and the runtime map decides
uniqueness — there is no hashing, no reflection and no custom equality contract.
As a result a Set can never silently drop an element to a collision, and Len
always reflects the true number of distinct elements.
- Generic over any
comparableelement type; exact==identity. - Full set algebra (
Union,Intersection,Difference,SymmetricDifference) and relations (Equal,IsSubset,IsSuperset,IsProperSubset,IsProperSuperset,IsDisjoint). - Functional helpers:
Map,Filter,Reduce,Fold,Any,All. iter.Seq[T]iteration forrange, plusAddSeq/Collect.- Usable zero value:
var s set.Set[int]is an empty, ready-to-use set. - JSON serialization through the standard
encoding/jsoninterfaces. - Zero dependencies.
go get github.com/goloop/set/v2import "github.com/goloop/set/v2"Requires Go 1.24 or newer. The package has no third-party dependencies.
package main
import (
"fmt"
"github.com/goloop/set/v2"
)
func main() {
ints := set.New[int]() // empty; element type explicit
words := set.New("one", "two") // or inferred from the elements
ints.Add(1, 2, 3, 4)
words.Add("two", "three") // "two" is already present
fmt.Println(ints.Len()) // 4
fmt.Println(set.Sorted(ints)) // [1 2 3 4]
fmt.Println(ints.Contains(3)) // true
// Set algebra (each accepts several sets at once).
a := set.New(1, 3, 5, 7)
b := set.New(0, 2, 4, 7)
fmt.Println(set.Sorted(a.Intersection(b))) // [7]
fmt.Println(a.IsDisjoint(set.New(8, 9))) // true
}A
Setis not safe for concurrent use, like the built-in map it is built on — synchronize externally if a shared set is mutated from several goroutines.
- Full reference and recipes: DOC.md · DOC.UK.md
- Package API: pkg.go.dev/github.com/goloop/set/v2
- Changes between versions: CHANGELOG.md
Contributions are welcome. Please run go test ./..., go vet ./... and
gofmt -l . before submitting a pull request.
set is released under the MIT License. See LICENSE.