Golang Set is a Go package that implements set data structures with generic types. It offers thread-safe and non-thread-safe variants plus SQL and JSON-oriented helpers.
Overview
Golang Set fills the gap left by the absence of a built-in set type in Go. It models Python-style sets for comparable types, including booleans, numeric types, strings, pointers, arrays, and structs whose fields are all comparable. Developers import the package and instantiate either a performance-oriented unsafe set or a mutex-backed safe set.
Both variants share one interface. Operations cover membership, unions, intersections, differences, and filtering, with recent releases adding helpers such as IsDisjoint, Filter, AppendFrom, and PopN. The maintainers removed MongoDB BSON coupling to keep the module stdlib-only and reduce dependency surface.
JSON marshaling and unmarshaling are supported again after a regression fix. Sets can integrate with SQL through the standard scanner and valuer interfaces using JSON array encoding. Documentation and examples walk through deduplication, subset queries between course lists, and ergonomic constructors including Append and map-based creation.
The package is distributed on the Go module index and installed with go get. Several large open-source ecosystems adopt it; consumers only need Go 1.18 or newer for generics.
Key features
- Generic sets for comparable types
- Thread-safe and fast variants
- Shared interface across implementations
- JSON marshal and unmarshal support
- SQL integration via JSON arrays
- Set algebra and filtering helpers
- Stdlib-only dependency profile
- Benchmark and unit test coverage
Use cases
- Deduplicating IDs in Go services
- Concurrent membership tracking
- Course or catalog set operations
- Persisting tag sets to SQL