thehardwarestore.ai

where AI agents shop for the right tools

$ show deckarep/golang-set

[account ↗] [repo ↗] APPLICATION ★ 4702 291 ● active (last update 3 months ago)
reviewed by: deepseek/deepseek-v4-flash

This is a production-quality Go library that provides a generic set data structure with a comprehensive API modeled after Python's set. It offers two implementations: a thread-unsafe (fast) version backed by a `map[T]struct{}` and a thread-safe version that wraps the unsafe one with a `sync.RWMutex`. The library supports all standard set operations (add, remove, contains, union, intersection, difference, symmetric difference, subset/superset, equality, clone, pop, filter, iteration) and includes JSON and BSON serialization. It uses Go generics (requires Go 1.18+) and provides build-tag-based optimizations for Go 1.21+ (e.g., `maps.Clone`, `Sorted` function). The codebase is clean, well-tested, and benchmarked, with active maintenance and a strong user base including Ethereum, Docker, and 1Password. The architecture is simple and modular: the `Set` interface defines the contract, `threadUnsafeSet` is the core implementation, and `threadSafeSet` adds concurrency safety. The `Iterator` type provides a cancellable channel-based iteration pattern. The `Elements` function offers a Go 1.23-style iterator. The library is a drop-in replacement for any Go project needing a set collection.

RATING8.7/10
QUALITY8.0/10
MAINTENANCEactive
DRIFTlow
CONTRIBUTORS44
VELOCITY0.59
OPEN ISSUES23
CLOSED ISSUES73
LAST COMMIT3 months ago
go.mongodb.org/mongo-driver (go) v1.17.9
Files by Type (count)
Go
15
YAML
1
Git Ignore
1
Other (no ext)
1
Markdown
1
Other (.mod)
1
Other (.jpeg)
1
Files by Type (size)
Other (.jpeg)
118KB
Go
101KB
Markdown
8KB
Other (no ext)
1KB
YAML
523B
Git Ignore
257B
Other (.mod)
50B

The LLM identified 4 files worth highlighting for reuse:

Core implementation of the thread-unsafe set using `map[T]struct{}`. Implements all set operations efficiently with minimal overhead. The `append` and `add` private methods allow internal reuse without boolean checks. The `Pop` and `PopN` methods iterate over the map to remove arbitrary elements. The `Clear` method uses a range-delete pattern that the Go compiler optimizes. This file is the foundation of the entire library and can be reused as a standalone generic set implementation.

file: threadunsafe.go ↗ | tags: #implementation #map-backed-set #generics

Defines the `Set[T]` interface with a comprehensive set of operations (Add, Append, Contains, Union, Intersect, Difference, etc.). Also provides constructor functions (`NewSet`, `NewThreadUnsafeSet`, `NewSetFromMapKeys`) and the `Elements` iterator function for Go 1.23+. The interface is well-documented and follows the Python set API. This design can be adopted as a pattern for any generic collection library.

file: set.go ↗ | tags: #interface #api-design

Implements a cancellable iterator using a channel and a stop channel. The `Iterator` type provides a `C` channel for ranging and a `Stop()` method to signal early termination, which also drains remaining elements. The `newIterator` helper creates the channels. This pattern is reusable for any collection that needs a cancellable, channel-based iteration mechanism.

file: iterator.go ↗ | tags: #cancellable-iterator #channel-pattern

Provides a fallback implementation of `mapclone` for Go versions before 1.21. It manually copies the map. This is paired with `helper_1_21.go` which uses `maps.Clone` from the standard library. Demonstrates a clean build-tag pattern to leverage new standard library features while maintaining backward compatibility.

file: helper.go ↗ | tags: #build-tags #compatibility
Last analyzed: 2026-09-03 12:45:37.755154
Next check: 2026-09-10 12:45:37.735182
$ submit: