tools

package module
v0.0.0-...-bc7e5c2 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: CC0-1.0 Imports: 16 Imported by: 1

Documentation

Index

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

func FormatDuration(d time.Duration) string

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 HTTPClient() *http.Client

func Includes

func Includes[T comparable](values []T, value T) bool

Includes returns true if the slice contains the given element.

func IsNotZero

func IsNotZero[T any](v T) bool

IsNotZero checks whether the given value is different from the default value for its type.

func IsOff

func IsOff(v interface{}, deflt bool) bool

IsOff checks if the given value indicates a disabled state. If the state cannot be determined, it returns the provided default value.

func IsOn

func IsOn(v interface{}, deflt bool) bool

IsOn checks if the given value indicates an enabled state. If the state cannot be determined, it returns the provided default value.

func IsZero

func IsZero[T any](v T) bool

IsZero checks whether the given value has the default value for its type.

func Keys

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

Keys returns the keys of a map.

func LoadJSON

func LoadJSON(file string, v interface{}) error

LoadJSON decodes JSON read from the given file.

func Map

func Map[T any](values []T, funcs ...MapFunc[T]) []T

Map applies mapping functions sequentially to each value and returns the result.

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

func ParseDuration(input string) (time.Duration, error)

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

func ParseDurationWithDefaultUnit(input, defaultUnit string) (time.Duration, error)

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

func ResolveFiles(path string) ([]string, error)

ResolveFiles resolves the given path to all existing files, see ResolvePath.

func ResolvePath

func ResolvePath(path string) ([]string, error)

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

func SaveFile(file string, data []byte, perm os.FileMode) error

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 SaveFileFunc(file string, f func(w io.Writer) error, perm os.FileMode) error

func SaveJSON

func SaveJSON(file string, v interface{}, indented bool, perm os.FileMode) error

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

func SortNatural[T ~string](values []T, ignoreCase bool) []T

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.

type MapFunc

type MapFunc[T any] func(T) T

type SelectFunc

type SelectFunc[T any] func(T) bool

Jump to

Keyboard shortcuts

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