stdlibex

package
v0.0.0-...-60c5125 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2025 License: MIT Imports: 20 Imported by: 0

README

The stdlibex package

Author ~Date Email
Mike Schinkel Oct 2024 mike@newclarity.net

Standard Library Extensions

This package contains Go functions that would be nice if they or an equivalent were built into Go's standard library, but since they are not this package is the consolation prize.

All of these functions independent — many of which leverage GoLang's generics feature — that the author considers an extension and wishes were part of the Go standard library.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLanguage = language.English

Functions

func AllInSlice

func AllInSlice[S []T, T comparable](s1, s2 S) bool

func AnyInSlice

func AnyInSlice[S []T, T comparable](s1, s2 S) (anyIn bool)

func AsciiToLower

func AsciiToLower(s any) string

AsciiToLower lowercases ASCII strings in a manner slightly more performant than strings.ToLower().

func AsciiToUpper

func AsciiToUpper(s any) string

AsciiToUpper uppercases ASCII strings in a manner slightly more performant than strings.ToUpper().

func ContainsNil

func ContainsNil(value any) (contains bool)

ContainsNil returns true if an interface contains nil (vs. ==nil)

func ContainsSameElements

func ContainsSameElements[E1, E2 any, C comparable](s1 []E1, fn1 func(E1) C, s2 []E2, fn2 func(E2) C) (same bool)

ContainsSameElements Compares two 'value' arrays — 'value' meaning containing no elements that are references such as pointers, slices, maps or channels — and return true if they have all the same elements and no more, even if the elements are in different positions in the slice.

func DedupSlicesFunc

func DedupSlicesFunc[S ~[]E, E any](s S, cmpFunc func(E, E) int) S

DedupSlicesFunc removes duplicate slices based on the return value of cmpFunc

DedupSlicesFunc([]int{0,2,4,2,3,7,3,2,8},fn) => []int{0,2,4,3,7,8}

Note the example uses scalars but this is typically to be used for slices of objects

func DeleteElement

func DeleteElement[T any](s []T, pos int) []T

func DiffSlices

func DiffSlices[S ~[]E, E comparable](s1, s2 S) (diff S)

DiffSlices takes two slices and collects any elements FOUND in the 1st slice that are NOT IN the 2nd slice, e.g.

DiffSlices([]int{0,2,4,6,8},[]int{1,2,3,4,5,6,7},fn) => []int{0,8}

func DiffSlicesFunc

func DiffSlicesFunc[S ~[]E, E any](s1, s2 S, idFunc func(E) string) (diff S)

DiffSlicesFunc takes two slices and collects any elements FOUND in the 1st slice that are NOT IN the 2nd slice, e.g.

DiffSlicesFunc([]int{0,2,4,6,8},[]int{1,2,3,4,5,6,7},fn) => []int{0,8}

Note the example uses scalars but this is typically to be used for slices of objects

func DirExists

func DirExists(dir string) (exists bool, err error)

func Empty

func Empty(value any) (empty bool)

func EnsureDir

func EnsureDir(dir string) (err error)

func EnsureFileRemoved

func EnsureFileRemoved(file string) (err error)

func EntryExists

func EntryExists(entry string) (exists bool, err error)

func FileAppendLine

func FileAppendLine(line string, filename string) (err error)

func FileExists

func FileExists(file string) (exists bool, err error)

func FileReadLines

func FileReadLines(filename string) (lines []string, err error)

func FileRemoveLine

func FileRemoveLine(line, filename string) (err error)

func FileSaveLines

func FileSaveLines(filename string, lines []string) (err error)

func FilterSlices

func FilterSlices[E comparable](s []E) []E

FilterSlices returns the slice with all non-zero elements removed.

FilterSlices([]string{"A", "", "CNAME"}) => []string{"A", "CNAME"}

func GetElementFunc

func GetElementFunc[E any](slice []E, isElementFn func(E) bool) (ele E, found bool)

func GetMissingElements

func GetMissingElements[E1, E2 any, C comparable](s1 []E1, fn1 func(E1) C, s2 []E2, fn2 func(E2) C) (missing []C)

GetMissingElements Compares two 'value' arrays — 'value' meaning containing no elements that are references such as pointers, slices, maps or channels — and returns the elements missing in s2 that exist in s1.

func IndentLines

func IndentLines(n int, s string) string

IndentLines each line of a string by `n` number of spaces Uses tab (`\t`) is n==0

func IntersectSlice

func IntersectSlice[T comparable](s1, s2 []T) (intersect []T)

IntersectSlice returns a slice containing all the elements that are in both s1 and s2.

func IsValidHostName

func IsValidHostName(domain Hostname) bool

func IsValidIPv4Address

func IsValidIPv4Address(ip IPv4Address) bool

func MakeKeysMap

func MakeKeysMap[K comparable](s []K) map[K]struct{}

MakeKeysMap takes a slice and returns a map[comparable]struct{} where each element from the slice becomes a map key.

func MapDefault

func MapDefault[K comparable, T any](m map[K]T, key K, def T) T

MapDefault takes a map and its comparable key and returns the key's associated value if it exists but if not returns the default value passed as a 3rd parameter. element from the slice becomes a map key.

func MapKeys

func MapKeys[M ~map[K]V, K comparable, V any](m M) []K

MapKeys returns the keys of the map m as a slice. The keys will be an indeterminate order.

func MapValues

func MapValues[M ~map[K]V, K comparable, V any](m M) []V

MapValues returns the values of the map m as a slice. The keys will be an indeterminate order.

func MarshalJSONFile

func MarshalJSONFile(_ Context, file string, object any) error

func MergeSlices

func MergeSlices[S ~[]E, E cmp.Ordered](s1, s2 S) S

MergeSlices takes two slices of the same element type and merges them, removing any duplicates

MergeSlices([]int{0,2,4,6,8},[]int{1,2,3,4,5,6,7},fn) => []int{0,1,2,3,4,5,6,7,8}

func MergeSlicesFunc

func MergeSlicesFunc[S ~[]E, E any](s1, s2 S, cmpFunc func(E, E) int) S

MergeSlicesFunc takes two slices of the same element type and merges them, removing any duplicates

MergeSlicesFunc([]int{0,2,4,6,8},[]int{1,2,3,4,5,6,7},fn) => []int{0,1,2,3,4,5,6,7,8}

Note the example uses scalars but this is typically to be used for slices of objects

func Must

func Must[T any](t T, err error) T

func MustClose

func MustClose(c io.Closer)

func Ptr

func Ptr[T any](t T) *T

func RemoveElementFunc

func RemoveElementFunc[T any](slice []T, isElementFn func(T) bool) []T

func RemoveElements

func RemoveElements[S []T, T any](slice S, startPos, count int) S

func SlicesIntersect

func SlicesIntersect[T comparable](s1, s2 []T) (intersects bool)

func SpliceElements

func SpliceElements[S []T, T any](slice S, startPos, count int, ele ...T) S

func Title

func Title(s string) string

func TouchFile

func TouchFile(path string) (err error)

func UnmarshalJSONFile

func UnmarshalJSONFile(_ Context, filename string, object any) error

Types

type CallStack

type CallStack struct {
	Frames []Frame
}

func Callers

func Callers(depth int) (callStack CallStack)

type Context

type Context = context.Context

type Frame

type Frame runtime.Frame

func Caller

func Caller(depth int) (frame Frame)

type Hostname

type Hostname string

type IPv4Address

type IPv4Address string

type String

type String string

func (String) String

func (s String) String() string

Jump to

Keyboard shortcuts

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