utils

package
v1.8.18 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable](s []T, e T) bool

func DurationOrDefault added in v1.3.0

func DurationOrDefault(value, defaultValue time.Duration) time.Duration

func EncodeURLParams

func EncodeURLParams(q any) string

func Filter

func Filter[T any](ss []T, test func(T, int) bool) (res []T)

func Find

func Find[T any](ss []T, test func(T, int) bool) (res T)

func FirstValue

func FirstValue(values ...string) string

func Foreach

func Foreach[T any](ss []T, apply func(T, int) T) []T

func GetContentTypeFromURL added in v1.8.4

func GetContentTypeFromURL(fileURL string) string

GetContentTypeFromURL tries to infer content type from file extension in URL

func GetExtensionFromContentType added in v1.8.4

func GetExtensionFromContentType(contentType string) string

GetExtensionFromContentType returns file extension based on content type

func HasMatch added in v1.8.16

func HasMatch(text string, patterns []string) bool

Match checks if text matches any of the provided regex patterns

func Hash

func Hash(s string, l int) string

func If

func If[T any](condition bool, trueValue, falseValue T) T

func IntOrDefault

func IntOrDefault(value, defaultValue int) int

func Map

func Map[T, V any](ts []T, fn func(T) V) []V

func Matches added in v1.8.16

func Matches(pattern string, str string) (map[string]string, error)

func MergeObjects added in v1.3.0

func MergeObjects[T any](objA *T, objB T)

MergeObjects merges two objects of the same type. It will iterate over the fields of the object and set the value of objA to the value of objB if the value of objA is the zero value.

func ParseInt added in v1.0.3

func ParseInt(s string) int

func ParseJson

func ParseJson[T interface{}](jsonStr string) (T, error)

func ParseTimeout added in v1.0.3

func ParseTimeout(s string) time.Duration

func RandomInt

func RandomInt(min, max int) int

func RandomString

func RandomString(length int) string

func RegexSubMatch

func RegexSubMatch(r *regexp.Regexp, str string) map[string]string

func Retry

func Retry[T any](ctx context.Context, retries int, delay time.Duration, f func(retryCount int) (T, error)) (T, error)

func Sleep

func Sleep(ctx context.Context, delay time.Duration)

func SleepRandom

func SleepRandom(ctx context.Context, min, max int)

func StartsWith

func StartsWith(s, prefix string) bool

func StringOrDefault

func StringOrDefault(value, defaultValue string) string

func StringToIntOrDefault

func StringToIntOrDefault(value string, defaultValue int) int

func StripNonAlphanumeric added in v1.8.9

func StripNonAlphanumeric(s string) string

func StripWhitespace

func StripWhitespace(s string) string

func Try

func Try(f func(), logger ...log.LoggerInterface)

Try wraps a goroutine and will recover from a panic If a logger is provided, it will log the error using the given logger

func TryCatch

func TryCatch(f func(), catch func(e error, stackTrace string))

TryCatch wraps a goroutine and will recover from a panic It will pass the error message to the catch function on panic

func TryReturn

func TryReturn[T any](f func() (T, error)) (res T, err error)

TryReturn wraps a function with a return value and will recover from a panic by returning an error

Types

This section is empty.

Jump to

Keyboard shortcuts

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