Replace a hand-rolled map-based set in a Go project with a well-tested, typed library.
Compute the intersection or difference between two data collections when deduplicating or comparing items in a Go service.
Share a set safely between multiple goroutines using the thread-safe variant without writing your own locking code.
Requires Go 1.18 or newer for generics, iterator support requires Go 1.23 or newer.
golang-set is a library that adds a set data structure to the Go programming language. A set is a collection that holds unique items and makes it easy to ask questions like: does this collection contain a specific item? Which items appear in both of these collections? Which items are in one collection but not the other? Go does not include a set type in its standard library, so developers typically build one themselves using maps. This library provides a ready-made, well-tested implementation instead. The library was inspired by the set type in Python, and it mirrors Python's set operations: union (combine two sets), intersection (items in common), difference (items in one set but not the other), and cardinality (count of items). The README shows a worked example using school courses, creating separate sets for required classes, science classes, electives, and bonus courses, then combining and querying them to illustrate how the operations work. Two variants are available. One is optimized for speed in single-threaded programs. The other is thread-safe, meaning it can be used safely when multiple goroutines might read and write the set at the same time. Both share the same interface, so you can switch between them without changing the rest of your code. The library uses Go generics, which means you specify what type of items the set will hold when you create it: integers, strings, structs, or any other comparable type. This requires Go version 1.18 or newer. Recent versions added iterator support for Go 1.23 and BSON serialization support. The project is used by organizations including Docker, Ethereum, 1Password, and Hashicorp. It is actively maintained and includes a thorough test and benchmark suite.
← deckarep on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.