go-generics
go-generics copied to clipboard
Go-generics - Generic slice, map, set, iterator, and goroutine utilities for Go
This is go-generics, a collection of typesafe generic utilities for slices, maps, sets, iterators, and goroutine patterns in Go.
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.
It also includes combinatorial operations:
Permutations, Combinations, and CombinationsWithReplacement.
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.
Maps
The maps package has a few convenience functions
for duplicating, inverting, constructing, and iterating over maps,
as well as for testing their equality.
The maps package is a drop-in replacement
for the maps package
added to the Go stdlib
in Go 1.21.
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.
Iter
The iter package implements efficient, typesafe iterators
that can convert to and from Go slices, maps, and channels,
and produce iterators over the values from function calls and goroutines.
There is also an iterator over the results of a SQL query;
the usual collection of functions on iterators
(Filter, Map, Concat, Accum, etc.).
Parallel
The parallel package contains functions for coordinating parallel workers:
Consumersmanages a set of N workers consuming a stream of values produced by the caller.Producersmanages a set of N workers producing a stream of values consumed by the caller.Valuesconcurrently produces a set of N values.Poolmanages access to a pool of concurrent workers.Protectmanages concurrent access to a protected data value.