gotils

package module
v1.10.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2025 License: MIT Imports: 13 Imported by: 0

README

GoTils

My Golang utils library.

TODO

  • Get full test coverage.
  • Unique functions should change the slice, not create a new one.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDecodingFile = errors.New("decoding file failed")
	ErrEncodingFile = errors.New("encoding file failed")
	ErrOpeningFile  = errors.New("opening file failed")
)

Functions

func ApplyToSlice

func ApplyToSlice[T any](array []T, funcs ...func(T) T)

ApplyToSlice applies all functions to the slice

func Clamp added in v1.10.0

func Clamp[T cmp.Ordered](lower, upper, value T) T

func CloneSlice

func CloneSlice[T any](array []T) []T

CloneSlice returns a new slice with the same values.

func CollectChannelMessages added in v1.10.0

func CollectChannelMessages[T any](ctx context.Context, c chan T) ([]T, error)

CollectChannelMessages pulls all messages from the channel, until the channel is closed or the context runs out

func CopyMap added in v1.8.0

func CopyMap[K comparable, V any](src map[K]V) map[K]V

func ExtendMap added in v1.8.0

func ExtendMap[K comparable, V any](dst map[K]V, srcs ...map[K]V)

func FilePathExists

func FilePathExists(filename string) (bool, error)

FilePathExists check if a file (or directory) exists

func FilterSlice

func FilterSlice[T any](array *[]T, filter func(T) bool)

FilterSlice removes all items from the slice that don't match the filter.

func GetMapKeys added in v1.8.0

func GetMapKeys[K comparable, V any](src map[K]V) []K

func GetMapValues added in v1.8.0

func GetMapValues[K comparable, V any](src map[K]V) []V

func MakeSliceUnique

func MakeSliceUnique[T comparable](array *[]T)

MakeSliceUnique removes all but the first appearance of each value.

func MakeSliceUniqueFunc

func MakeSliceUniqueFunc[T any](array *[]T, hasher func(v T) string)

MakeSliceUniqueFunc removes all but the first appearance of each value. Values are hashed using the hasher function.

func MakeSortedSliceUnique

func MakeSortedSliceUnique[T comparable](array *[]T)

MakeSortedSliceUnique removes all but the first appearance of each value for pre-sorted slices.

func MakeSortedSliceUniqueFunc

func MakeSortedSliceUniqueFunc[T any](array *[]T, equal func(a, b T) bool)

MakeSortedSliceUnique removes all but the first appearance of each value for pre-sorted slices. Values are compared using the equal function.

func MapSlice

func MapSlice[T any, R any](array []T, fn func(T) R) []R

MapSlice returns a new slice of mapped values.

func Max

func Max[T constraints.Ordered](a, b T) T

Max returns the larger value

func Min

func Min[T constraints.Ordered](a, b T) T

Min returns the smaller value

func ParallelFor added in v1.10.1

func ParallelFor[T any](ctx context.Context, values []T, coroutines int, fn func(context.Context, int, T) error) []error

ParallelFor iterates over the slice in parallel using Goroutines.

func ReadJSONFile

func ReadJSONFile(filename string, dst any) error

ReadJSONFile reads and decodes the file content into the destination

func ReduceSlice

func ReduceSlice[T any, R any](array []T, initial R, fn func(R, T) R) R

ReduceSlice returns the reduced value.

func SliceContainsAll

func SliceContainsAll[T comparable](haystack []T, needles []T) bool

SliceContainsAll returns true if all needles are present in the haystack.

func SliceContainsAny

func SliceContainsAny[T comparable](haystack []T, needles []T) bool

SliceContainsAny returns true if one needle is present in the haystack.

func SliceContainsNone

func SliceContainsNone[T comparable](haystack []T, needles []T) bool

SliceContainsNone returns true if no needles are present in the haystack.

func StringContainsAll

func StringContainsAll(s string, needles []string) bool

func StringContainsAny

func StringContainsAny(s string, needles []string) bool

func StringContainsNone

func StringContainsNone(s string, needles []string) bool

func StringMatchesAny

func StringMatchesAny(s string, needles []string) bool

func Try added in v1.10.0

func Try(ctx context.Context, maxAttempts int, waitDuration time.Duration, f func(ctx context.Context, attempt int) error) error

Try reruns the function until it succeeds or runs out of attempts

func Try2 added in v1.10.0

func Try2[T any](ctx context.Context, maxAttempts int, waitDuration time.Duration, f func(ctx context.Context, attempt int) (T, error)) (T, error)

Try2 reruns the function until it succeeds or runs out of attempts

func WriteJSONFile

func WriteJSONFile(filename string, indent string, data any, perm ...fs.FileMode) error

Types

type Counter added in v1.6.0

type Counter[T comparable] struct {
	// contains filtered or unexported fields
}

Counter is a thread safe way to count comparable things.

func NewCounter added in v1.6.0

func NewCounter[T comparable](keys []T) *Counter[T]

NewCounter creates a new Counter.

func (*Counter[T]) AddKey added in v1.6.0

func (c *Counter[T]) AddKey(key T)

AddKey adds a key if it doesn't exist already.

func (*Counter[T]) Clear added in v1.6.0

func (c *Counter[T]) Clear()

Clear removes all keys

func (*Counter[T]) Increment added in v1.6.0

func (c *Counter[T]) Increment(key T)

Increment adds 1 to the count. If the key doesn't exist, it's created.

func (*Counter[T]) IncrementBy added in v1.6.0

func (c *Counter[T]) IncrementBy(key T, value uint)

IncrementBy adds the value to the count. If the key doesn't exist, it's created.

func (*Counter[T]) IncrementByIfKeyExists added in v1.6.0

func (c *Counter[T]) IncrementByIfKeyExists(key T, value uint) bool

Increment adds the value to the count if the key exists and return true.

func (*Counter[T]) IncrementFunc added in v1.7.0

func (c *Counter[T]) IncrementFunc(fn func(key T, cnt uint) (uint, bool)) bool

IncrementFunc iterates over all existing keys. If the fn returned uint is greater than zero, the key will be incremented. If the fn returned bool is false, the iteration is stopped. It returns true if any key was incremented

func (*Counter[T]) IncrementIfKeyExists added in v1.6.0

func (c *Counter[T]) IncrementIfKeyExists(key T) bool

Increment adds 1 to the count if the key exists and return true.

func (*Counter[T]) KeyExists added in v1.6.0

func (c *Counter[T]) KeyExists(key T) bool

KeyExists checks if a key exists.

func (*Counter[T]) KeyExistsFunc added in v1.7.0

func (c *Counter[T]) KeyExistsFunc(fn func(key T) bool) bool

KeyExistsFunc iterates over all existing keys. If the func returns true, the iteration is stopped and true is returned, otherwise the function returns false.

func (*Counter[T]) Keys added in v1.7.0

func (c *Counter[T]) Keys() []T

Values returns the map of counts

func (*Counter[T]) Set added in v1.8.0

func (c *Counter[T]) Set(key T, cnt uint)

Set directly sets the count.

func (*Counter[T]) SetToZero added in v1.6.0

func (c *Counter[T]) SetToZero()

SetToZero sets all key counts to zero

func (*Counter[T]) Values added in v1.6.0

func (c *Counter[T]) Values() map[T]uint

Values returns the map of counts

type Maybe added in v1.9.0

type Maybe[T any] struct {
	Value T
	Valid bool
}

Maybe wraps a value and adds validity

type Result added in v1.10.0

type Result[T any] struct {
	Value T
	Err   error
}

Result joins a value and an error together

func ParallelMap added in v1.10.1

func ParallelMap[T any, R any](ctx context.Context, values []T, coroutines int, fn func(context.Context, int, T) (R, error)) []Result[R]

ParallelMap maps over the slice in parallel using Goroutines.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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