MCPcopy Index your code
hub / github.com/bobg/go-generics

github.com/bobg/go-generics @v4.2.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v4.2.0 ↗ · + Follow
212 symbols 632 edges 25 files 97 documented · 46%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Go-generics - Generic slice, map, set, iterator, and goroutine utilities for Go

Go Reference Go Report Card Tests Coverage Status Mentioned in Awesome Go

This is go-generics, a collection of typesafe generic utilities for slices, sets, and goroutine patterns in Go.

Compatibility note

This is version 4 of this library, for the release of Go 1.23.

Earlier versions of this library included a package, iter, that defined an iterator type over several types of containers, and functions for operating with iterators. However, Go 1.23 defines its own, better iterator mechanism via the new “range over function” language feature, plus a new standard-library package also called iter. This version of the go-generics library therefore does away with its iter package. The handy functions that iter contained for working with iterators (Filter, Map, FirstN, and many more) can now be found in the github.com/bobg/seqs library, adapted for Go 1.23 iterators.

(This version of go-generics might have kept iter as a drop-in replacement for the standard-library package, but was unable because the standard library defines two types, iter.Seq[K] and iter.Seq2[K, V], that go-generics would have had to reference using type aliases; but Go type aliases do not yet permit type parameters.)

Earlier versions of this library included combinatorial operations in the slices package. Those have now been moved to their own library, github.com/bobg/combo.

Earlier versions of this library included a maps package, which was a drop-in replacement for the stdlib maps (added in Go 1.21) plus a few convenience functions. With the advent of Go 1.23 iterators, those few convenience functions are mostly redundant (and a couple of them − Keys and Values − conflict with new functions in the standard library), so maps has been removed.

Earlier versions of this library included a Find method on the set.Of[T] type, for finding some element in the set that satisfies a given predicate. This method has been removed in favor of composing operations with functions from github.com/bobg/seqs. For example, s.Find(pred) can now be written as seqs.First(seqs.Filter(s.All(), pred)).

Slices

The slices package is useful in three ways:

  • It encapsulates hard-to-remember Go idioms for inserting and removing elements to and from the middle of a slice;
  • It adds the ability to index from the right end of a slice using negative integers (for example, Get(s, -1) is the same as s[len(s)-1]); and
  • It includes Map, Filter, and a few other such functions for processing slice elements with callbacks.

The slices package is a drop-in replacement for the slices package added to the Go stdlib in Go 1.21. There is one difference: this version of slices allows the index value passed to Insert, Delete, and Replace to be negative for counting backward from the end of the slice.

Set

The set package implements the usual collection of functions for sets: Intersect, Union, Diff, etc., as well as member functions for adding and removing items, checking for the presence of items, and iterating over items.

Parallel

The parallel package contains functions for coordinating parallel workers:

  • Consumers manages a set of N workers consuming a stream of values produced by the caller.
  • Producers manages a set of N workers producing a stream of values consumed by the caller.
  • Values concurrently produces a set of N values.
  • Pool manages access to a pool of concurrent workers.
  • Protect manages concurrent access to a protected data value.

Core symbols most depended-on inside this repo

Clone
called by 21
slices/dropin.go
Equal
called by 19
slices/dropin.go
Add
called by 14
set/set.go
Has
called by 11
set/set.go
Len
called by 10
set/set.go
Equal
called by 10
set/set.go
Error
called by 9
parallel/parallel.go
EqualFunc
called by 8
slices/dropin.go

Shape

Function 187
Method 17
Struct 6
TypeAlias 2

Languages

Go100%

Modules by API surface

slices/dropin_test.go60 symbols
slices/slices.go36 symbols
slices/dropin.go36 symbols
set/set.go16 symbols
slices/example_test.go13 symbols
slices/slice_test.go9 symbols
parallel/parallel.go7 symbols
parallel/example_test.go5 symbols
set/example_test.go4 symbols
parallel/parallel_test.go4 symbols
set/set_test.go3 symbols
slices/replace_test.go2 symbols

For agents

$ claude mcp add go-generics \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact