Categorygithub.com/mokiat/gog
modulepackage
0.13.1
Repository: https://github.com/mokiat/gog.git
Documentation: pkg.go.dev

# README

GoG (Go Generics)

Go Reference Go Report Card

GoG is a Go library with useful generic functions and types.

Since the introduction of generics in Go 1.18, a number of useful and reusable data structures, algorithms and utility functions are now possible. This library attempts to cover some of the most common use cases.

It avoids duplicating functions that are already provided by slices and maps.

For a complete list on available functions and types, check the godoc documentation for this project:

Examples

Converting slice values from one type to another:

source := []int{1, 2, 3, 4}
target := gog.Map(source, func(item int) float64 {
  return float64(item) / 2.0
})
// target = []float64{0.5, 1.0, 1.5, 2.0}

Removing duplicate items in a slice:

source := []string{"john", "bill", "eric", "john", "max", "eric"}
target := gog.Dedupe(source)
// target = []string{"john", "bill", "eric", "max"}

Finding an item in a slice:

source := []string{"user 01", "user 02", "user 05", "user 33"}
foundItem, ok := gog.FindFunc(source, func(item string) bool {
  return strings.Contains(item, "02")
})
// ok = true
// foundItem = "user 02"

Selecting specific slice items:

source := []int{1, 2, 3, 4}
target := gog.Select(source, func(item int) bool {
  return item % 2 == 0
})
// target = []int{2, 4}

# Packages

Package ds provides various data structure implementations with generics.
Package filter adds helper functions for performing filtering over a data set.
Package opt provides utilities for representing optional types.

# Functions

Concat takes a series of slices and concatenates them into one single slice.
Dedupe returns a new slice that contains only distinct elements from the original slice.
DerefElements converts a slice of element pointers into a slice of those elements.
Entries returns a slice of key-value pair mappings that make up the specified map.
FindFunc iterates over the slice and uses the provided closure function to check whether the elements match a user-provided condition.
FindFuncPtr iterates over the slice and uses the provided closure function to check whether the elements match a user-provided condition.
Flatten returns a new slice that is the result of merging all nested slices into a single top-level slice.
Map can be used to transform one slice into another by providing a function to do the mapping.
MapIndex is similar to Map, except that it passes the element index to the closure function as well.
Mapping is similar to Partition, except that it allows one to transform the values stored in the partition buckets.
Merge takes a series of maps and merges them into a single map.
Mutate iterates over the slice and calls the provided closure function, passing a pointer to each element.
MutateIndex is similar to Mutate, except that it passes the element index to the closure function as well.
Partition splits the specified slice into groups, each having a key according to the specified function.
PtrOf returns a pointer to the passed value.
Reduce compacts a slice into a single value.
RefElements converts a slice of elements into a slice of pointers to those same elements.
Select returns a new slice that contains only the elements of the original slice that pass the filter function.
ValueOf is the opposite of PtrOf.
Zero returns the zero value of the generic type T.

# Structs

KV represents a key-value pair.