ks

package
v2.15.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ks ("kitchen sink") implements assorted helpful things that don't fit anywhere else.

Index

Constants

This section is empty.

Variables

View Source
var ErrTODO = errors.New("TODO")

Functions

func Checksum64 added in v2.11.0

func Checksum64(crc uint64, value uint64) uint64

func FilterError added in v2.8.0

func FilterError(err error, ignore ...error) error

FilterError returns err, unless errors.Is(err, i) returns true for any i in ignore, in which case it returns nil.

For example,

// Create a symlink but ignore an error if the file exists.
err := FilterError(os.Symlink(oldname, newname), fs.ErrExist)

func LiftErrorFunc added in v2.11.0

func LiftErrorFunc[X any](f func(x X) error) func(err error, x X) error

LiftErrorFunc takes any 1-arity function "f(x) => error", and returns a new function that takes an additional error input. If that error is not nil, it is returned immediately before calling f. Otherwise, the result of calling f(x) normally is returned.

This allows multiple simple error-returning functions to be called in sequence, with only one error check at the end.

func MustMap added in v2.7.0

func MustMap[K comparable, V any](m map[K]V) map[K]V

MustMap is used to construct a map if it is nil, or return the input unchanged (i.e. the identity function) if it is not nil. This is useful for conditionally initialising a map that may or may not be its zero value.

func Reserve deprecated added in v2.6.0

func Reserve[T any](xs []T, size int) []T

Reserve grows a slice, if necessary, to fit at least size extra elements.

Deprecated: use slices.Grow.

func SetLength added in v2.13.0

func SetLength[T any](xs []T, size int) []T

SetLength grows a slice, if necessary, so that has a capacity of at least size elements, and a length of exactly size elements. Any trailing elements in the underlying array that fall beyond the original capacity are zeroed.

func WrapBlock added in v2.0.1

func WrapBlock(message string, columns int) string

WrapBlock word-wraps a whitespace-delimited string to a given number of columns. The column length is given in runes (Unicode code points), not bytes.

This is a simple implementation without any configuration options, designed for circumstances such as quickly wrapping a single error message for display.

Save for bug fixes, the output of this function for any given input is frozen and will not be changed in future. This means you can reliably test against the return value of this function without your tests being brittle.

Caveat: Single words longer than the column length will be truncated.

Caveat: all whitespace, including existing new lines, is collapsed. An input consisting of multiple paragraphs will be wrapped into a single word-wrapped paragraph.

Caveat: assumes all runes in the input string represent a glyph of length one. Whether this is true or not depends on how the display and font treats different runes. For example, some runes where [Unicode.IsGraphic] returns false might still be displayed as a special escaped character. Some letters might be displayed wider than usual, even in a monospaced font.

Types

This section is empty.

Jump to

Keyboard shortcuts

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