Documentation ¶
Index ¶
- func AtExit(f func()) (cancel func())
- func Exit(code int)
- func Fail(format string, a ...interface{})
- func FirstNonEmpty[T any](values ...T) T
- func FormatDuration(d time.Duration) string
- func HTTPClient() *http.Client
- func Includes[T comparable](values []T, value T) bool
- func IsNotZero[T any](v T) bool
- func IsOff(v interface{}, deflt bool) bool
- func IsOn(v interface{}, deflt bool) bool
- func IsZero[T any](v T) bool
- func Keys[M ~map[K]V, K comparable, V any](m M) []K
- func LoadJSON(file string, v interface{}) error
- func Map[T any](values []T, funcs ...MapFunc[T]) []T
- func Merge[T comparable](slices ...[]T) []T
- func Minus[T comparable](s1, s2 []T) []T
- func ParseDuration(input string) (time.Duration, error)
- func ParseDurationWithDefaultUnit(input, defaultUnit string) (time.Duration, error)
- func ResolveFiles(path string) ([]string, error)
- func ResolvePath(path string) ([]string, error)
- func SaveFile(file string, data []byte, perm os.FileMode) error
- func SaveFileFunc(file string, f func(w io.Writer) error, perm os.FileMode) error
- func SaveJSON(file string, v interface{}, indented bool, perm os.FileMode) error
- func Select[T any](values []T, funcs ...SelectFunc[T]) []T
- func Sort[T constraints.Ordered](values []T) []T
- func SortNatural[T ~string](values []T, ignoreCase bool) []T
- func ToMap[K comparable, V any](keys []K, gen KeyValueGenerator[K, V]) map[K]V
- func ToMapWithValue[K comparable, V any](keys []K, v V) map[K]V
- func Tokens[T ~string](values ...T) []T
- func Unique[T comparable](values []T) []T
- func Values[M ~map[K]V, K comparable, V any](m M) []V
- type KeyValueGenerator
- type MapFunc
- type SelectFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AtExit ¶
func AtExit(f func()) (cancel func())
AtExit registers the given function to be run when Exit() is called. It returns a cancel function that allows to remove the exit function.
func Exit ¶
func Exit(code int)
Exit runs all registered exit functions in reverse order of their registration and then uses os.Exit to exit with the given code.
func Fail ¶
func Fail(format string, a ...interface{})
Fail formats according to a format specifier, writes to stderr and exits with code 1.
func FirstNonEmpty ¶
func FirstNonEmpty[T any](values ...T) T
FirstNonEmpty returns the first non-empty element of the given list. To use a fallback value, put it as the last element.
func FormatDuration ¶
FormatDuration takes a time.Duration and formats it as a string in the format used by ParseDuration. The format is the largest suitable unit followed by the next largest, and so on. For example, 25 hours 90 seconds is formatted as "1d1h1m30s".
func HTTPClient ¶
func Includes ¶
func Includes[T comparable](values []T, value T) bool
Includes returns true if the slice contains the given element.
func IsNotZero ¶
IsNotZero checks whether the given value is different from the default value for its type.
func IsOff ¶
IsOff checks if the given value indicates a disabled state. If the state cannot be determined, it returns the provided default value.
func IsOn ¶
IsOn checks if the given value indicates an enabled state. If the state cannot be determined, it returns the provided default value.
func Merge ¶
func Merge[T comparable](slices ...[]T) []T
Merge returns a new slice that includes all elements of all input slices. Duplicates are removed.
Example usage:
s1 := []int{1, 2} s2 := []int{2, 3, 4} result := Merge(s1, s2) // Output: [1, 2, 3, 4]
func Minus ¶
func Minus[T comparable](s1, s2 []T) []T
Minus is a generic function that returns a new slice including only those elements of the first input slice that are not present in the second input slice.
Example usage:
s1 := []int{1, 2, 3, 4} s2 := []int{1, 3} result := Minus(s1, s2) // Output: [2, 4]
func ParseDuration ¶
ParseDuration takes a string representing a duration and returns its equivalent time.Duration. It supports different units like seconds, minutes, hours, days, weeks and years.
func ParseDurationWithDefaultUnit ¶
ParseDurationWithDefaultUnit is similar to ParseDuration but it accepts a default unit. If the input string is a simple float, it assumes the default unit.
func ResolveFiles ¶
ResolveFiles resolves the given path to all existing files, see ResolvePath.
func ResolvePath ¶
ResolvePath resolves the given path. If it exist, it is returned. If it does not exist and does not contain any wildcard characters, os.ErrNotExist is returned. Otherwise, the result of filepath.Glob is returned. Unless the base of the glob pattern starts with a dot, entries stating with a dot are ignored.
func SaveFile ¶
SaveFile safely writes data to a file by writing it to a temporary file first before moving it over the destination file to ensure atomicity.
func SaveFileFunc ¶
func SaveJSON ¶
SaveJSON safely writes JSON encoded data to a file by encoding the given value to a temporary file first before moving it over the destination file. This should ensure atomicity.
func Select ¶
func Select[T any](values []T, funcs ...SelectFunc[T]) []T
Select returns these values for which any of the given select functions return true. If no select function is given, IsNotZero will be used to filter out empty elements.
func Sort ¶
func Sort[T constraints.Ordered](values []T) []T
Sort returns a sorted copy of a slice.
Example usage:
s := []int{3, 2, 4, 1} result := Sort(s) // Output: [1, 2, 3, 4]
func SortNatural ¶
SortNatural returns a naturally sorted copy of a slice of string type.
Example usage:
s := []string{"v1.10.3", "v1.5.1", "v1.10.1"} result := Sort(s) // Output: ["v1.5.1", "v1.10.1", "v1.10.3"]
func ToMap ¶
func ToMap[K comparable, V any](keys []K, gen KeyValueGenerator[K, V]) map[K]V
ToMap returns a map where each entry is the result of the generator function. Empty keys are ignored.
func ToMapWithValue ¶
func ToMapWithValue[K comparable, V any](keys []K, v V) map[K]V
ToMapWithValue returns a map with each key set to the given value.
func Tokens ¶
func Tokens[T ~string](values ...T) []T
Tokens splits the given values at whitespace or comma and returns lower-cased unique values.
func Unique ¶
func Unique[T comparable](values []T) []T
Unique returns a copy of the slice with all duplicates removed.
func Values ¶
func Values[M ~map[K]V, K comparable, V any](m M) []V
Values returns the values of a map.
Types ¶
type KeyValueGenerator ¶
type KeyValueGenerator[K comparable, V any] func(K) (K, V)
Define a function type that takes a Key and returns a Key and Value.