utils

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: MIT Imports: 16 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppName

func AppName() string

AppName returns the name of the executable that started this program (process).

func BatchSliceOfStrings

func BatchSliceOfStrings(ctx context.Context, keys []string, count int) <-chan []string

BatchSliceOfStrings groups the given keys into chunks of size count and streams them into a returned channel. Panics if count is less than or equal to zero.

func ChanFromSlice

func ChanFromSlice[T any](values []T) <-chan T

ChanFromSlice takes a slice of values and returns a channel from which these values can be received. This channel is closed after the last value was sent.

func Checksum

func Checksum(data interface{}) []byte

Checksum returns the SHA-1 checksum of the data.

func Ellipsize

func Ellipsize(s string, limit int) string

Ellipsize shortens s to <=limit runes and indicates shortening by "...".

func IsContextCanceled

func IsContextCanceled(err error) bool

IsContextCanceled returns whether the given error is context.Canceled.

func IsDeadlock

func IsDeadlock(err error) bool

IsDeadlock returns whether the given error signals serialization failure.

func IsUnixAddr

func IsUnixAddr(host string) bool

IsUnixAddr indicates whether the given host string represents a Unix socket address.

A host string that begins with a forward slash ('/') is considered Unix socket address.

func IterateOrderedMap added in v0.7.0

func IterateOrderedMap[K cmp.Ordered, V any](m map[K]V) iter.Seq2[K, V]

IterateOrderedMap implements iter.Seq2 to iterate over a map in the key's order.

This function returns a func yielding key-value-pairs from a given map in the order of their keys, if their type is cmp.Ordered.

func JoinHostPort

func JoinHostPort(host string, port int) string

JoinHostPort is like its equivalent in net., but handles UNIX sockets as well.

func PrintErrorThenExit

func PrintErrorThenExit(err error, exitCode int)

PrintErrorThenExit prints the given error to os.Stderr and exits with the specified error code.

func RawUrlEncode added in v0.8.0

func RawUrlEncode(s string) string

RawUrlEncode mimics PHP's rawurlencode to be used for parameter encoding.

Icinga Web uses rawurldecode instead of urldecode, which, as its main difference, does not honor the plus char ('+') as a valid substitution for space (' '). Unfortunately, Go's url.QueryEscape does this very substitution and url.PathEscape does a bit too less and has a misleading name on top.

func Timed

func Timed(start time.Time, callback func(elapsed time.Duration))

Timed calls the given callback with the time that has elapsed since the start.

Timed should be installed by defer:

func TimedExample(logger *zap.SugaredLogger) {
	defer utils.Timed(time.Now(), func(elapsed time.Duration) {
		logger.Debugf("Executed job in %s", elapsed)
	})
	job()
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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