explaingit

deckarep/golang-set

4,679GoAudience · developerComplexity · 2/5Setup · easy

TLDR

A ready-made set data structure for Go that fills the gap left by Go's standard library. Supports union, intersection, difference, and other set operations, with both a fast single-threaded and a thread-safe variant. Uses Go generics and requires Go 1.18 or newer.

Mindmap

mindmap
  root((golang-set))
    What it does
      Set data structure
      Union intersection difference
      Thread-safe variant
      Generic typed sets
    Tech Stack
      Go
      Go generics
    Use Cases
      Deduplicate data
      Compare collections
      Concurrent set access
    Adopters
      Docker
      Ethereum
      1Password
      Hashicorp
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Replace a hand-rolled map-based set in a Go project with a well-tested, typed library.

USE CASE 2

Compute the intersection or difference between two data collections when deduplicating or comparing items in a Go service.

USE CASE 3

Share a set safely between multiple goroutines using the thread-safe variant without writing your own locking code.

Tech stack

Go

Getting it running

Difficulty · easy Time to first run · 5min

Requires Go 1.18 or newer for generics, iterator support requires Go 1.23 or newer.

License not stated in the explanation.

In plain English

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.

Copy-paste prompts

Prompt 1
Show me how to use golang-set to find all items in one Go slice that are NOT in another slice.
Prompt 2
How do I create a thread-safe set of strings in Go using golang-set and update it from multiple goroutines?
Prompt 3
Using golang-set with generics, how do I compute the union and intersection of two sets of integers?
Prompt 4
How do I iterate over every item in a golang-set in Go 1.23 using the new iterator support?
Open on GitHub → Explain another repo

← deckarep on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.