gen

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2024 License: Apache-2.0 Imports: 2 Imported by: 1

README

gen - Generic Go Utility Library

Go Report Card Go Reference PRs Welcome

gen is a generic general use Go functions library with the intention of replacing duplicated code where the same functionality is needed across multiple types, and provides a common interface for the functionality.

The library is designed to be used in a wide variety of projects and is designed to be easy to use.

Installation

go get -u go.devnw.com/gen@latest

Import

Example:


import "go.devnw.com/gen"

func main() {
    // ...

    m := gen.Map[string, string]{
        "foo": "bar",
        "bar": "baz",
    }

    // Get a slice of map keys
    keys := m.Keys()

    // ...

    slice1 := []string{"foo", "bar"}
    slice2 := []string{"floob", "bar"}

    // Call the Unique method without the `gen` prefix.
    unique := gen.Unique(slice1, slice2)

    existingmap := map[string]int{
        "foo": 1223,
        "bar": 111,
    }

    // Cast to the generic Map
    m := gen.Map[string, int](existingmap)

    keys = m.Keys()
    values := m.Values()

    // ...

    existingSlice := []string{"foo", "bar", "baz"}

    // Cast to the generic Slice
    s := gen.Slice[string](existingSlice)

    // Convert slice to map

    m := s.Map()
}

Benchmarks

To execute the benchmarks, run the following command:

go test -bench=. ./...

To view benchmarks over time for the main branch of the repository they can be seen on our Benchmark Report Card.

Documentation

Overview

Package gen is a generic general use Go functions library which can be used throughout any project.

As a rule the methods in this package are non-destructive and return a new value rather than modifying the original.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As[T any](in ...T) []T

As allows you to cast N values passed in through a variadic argument to a slice of N values. This is useful for casting disparate struct types to slices of implemented interfaces

Example: As[io.Reader](&bytes.Buffer{}, &bufio.Reader{})

func Close

func Close[U channel[T], T any](in ...U)

Close closes all channels in the input slice

NOTE: If a channel is already closed any panic will be ignored and the channel will be skipped

func Compare

func Compare[T comparable](a, b []T) error

Compare returns nil if the two input slices are equal otherwise it returns an error indicating the issue

func Diff

func Diff[T comparable](a, b []T) []T

Diff returns the symmetric difference between the two input slices

func Equal

func Equal[T comparable](a, b []T) bool

Equal returns true if the two input slices are exactly equal

func Exclude

func Exclude[T comparable](a []T, b ...T) []T

Exclude returns a slice of values from the input slice minus the values supplied in the second argument

func Has

func Has[T comparable](in []T, v T) bool

Has determines if the input slice contains the input value

func Index

func Index[T comparable](in []T, v T) int

Index returns the index of the first occurrence of the input value

func Indices

func Indices[T comparable](in []T, v T) []int

Indices returns a slice of indices for all occurrences of value `v`

func Intersect

func Intersect[T comparable](a, b []T) []T

Intersect returns the intersection between the two input slices

func Match

func Match[T comparable](a, b []T) bool

Match returns true if the two input slices contain equivalent values NOTE: Match ignores ordering

func ReadOnly

func ReadOnly[U chan T, T any](in ...U) []<-chan T

ReadOnly accepts a slice of channels and returns a slice of channels that are read-only.

func Unique

func Unique[U ~[]T, T comparable](in U) []T

Unique returns a slice of unique values from the input slice

func WriteOnly

func WriteOnly[U chan T, T any](in ...U) []chan<- T

WriteOnly accepts a slice of channels and returns a slice of channels that are write-only.

Types

type FMap

type FMap[K, V comparable] Map[K, V]

FMap is a flippale map where the key and value can be swapped

func (FMap[K, V]) Flip

func (m FMap[K, V]) Flip() FMap[V, K]

Flip returns a new FMap with the key and value swapped

type IndexMismatchError added in v1.2.0

type IndexMismatchError[T any] struct {
	I        int
	Expected T
	Actual   T
}

func (IndexMismatchError[T]) Error added in v1.2.0

func (e IndexMismatchError[T]) Error() string

type LengthMismatchError added in v1.2.0

type LengthMismatchError struct {
	Expected int
	Actual   int
}

func (LengthMismatchError) Error added in v1.2.0

func (e LengthMismatchError) Error() string

type Map

type Map[K comparable, V any] map[K]V

Map wraps the Go map type in a generic which provides helper functions for accessing slices of keys or values without repeating the the implementation for each type

func (Map[K, V]) Keys

func (m Map[K, V]) Keys() []K

Keys returns a slice of keys from the map

func (Map[K, V]) Values

func (m Map[K, V]) Values() []V

Values returns a slice of values from the map

type Slice

type Slice[T comparable] []T

Slice wraps a slice of values with helper functions

func (Slice[T]) Chan

func (s Slice[T]) Chan(ctx context.Context) <-chan T

Chan converts the slice to a channel of type T

NOTE: This function does NOT use a buffered channel.

func (Slice[T]) Map

func (s Slice[T]) Map() Map[T, any]

Map returns a map of the given slice where the values of the slice are the Map keys

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL