Documentation
¶
Index ¶
- Constants
- Variables
- func AppendByte[T Number](dst []byte, i T) []byte
- func AppendUnicodeBar(b []byte, width int) []byte
- func Assert(cond bool, a ...any)
- func AssertNonZero[T comparable](v T) T
- func AssertNotNil[T any](v *T) *T
- func AssertOK[T any](v T, ok bool) T
- func AssertPositive[T Number](v T) T
- func BarChart[M ~map[K]V, K cmp.Ordered, V Number](w io.Writer, m M, title string, maxItems int)
- func Check(err error)
- func Clamp[T Number](val, min, max T) T
- func Close(v io.Closer)
- func CloseAfterRead(r io.ReadCloser) io.Reader
- func Closing(v io.Closer)
- func CopyDir(src, dst string) error
- func CopyFile(src, dst string) (int64, error)
- func DebugInfos() string
- func DecodeJSON(r io.Reader, v any) error
- func Filter[S ~[]E, E any](s S, keep func(E) bool) S
- func FormatByte[T Number](i T) string
- func Goroutines(nb int, fn func(i int)) (wait func())
- func Has[M ~map[K]V, K comparable, V any](m M, k K) bool
- func InitUnicodeCategory()
- func Intn[T Integer](n T) T
- func Keys[M ~map[K]V, K comparable, V any](m M) []K
- func Limit[V any](n int, seq iter.Seq[V]) iter.Seq[V]
- func Limit2[K, V any](n int, seq iter.Seq2[K, V]) iter.Seq2[K, V]
- func Map[S ~[]E, E, F any](s S, f func(E) F) []F
- func MultiLines(s string) string
- func Must[T any](a T, err error) T
- func Must2[T1, T2 any](a T1, b T2, err error) (T1, T2)
- func One[M ~map[K]V, K comparable, V any](m M) (K, V)
- func Output(c *exec.Cmd) ([]byte, error)
- func Ptr[T any](v T) *T
- func RandomBytes(n int) []byte
- func Run(c *exec.Cmd) error
- func Set[M ~map[K]V, K comparable, V any](m M, k K, v V) bool
- func Shuffle[S ~[]E, E any](s S)
- func Skip[V any](n int, seq iter.Seq[V]) iter.Seq[V]
- func Skip2[K, V any](n int, seq iter.Seq2[K, V]) iter.Seq2[K, V]
- func Sort[S ~[]E, E cmp.Ordered](x S) S
- func StdinIsPipe() bool
- func StdoutIsTerminal() bool
- func ToSet[S ~[]E, E comparable](s S) map[E]struct{}
- func UnicodeBar(width int) string
- func UnicodeCategory(r rune) (name string)
- func Values[M ~map[K]V, K comparable, V any](m M) []V
- type Float
- type Integer
- type Number
- type Protected
Constants ¶
const GenericError genericErr = false
Variables ¶
var StartTime = time.Now().UTC()
Functions ¶
func AppendByte ¶
AppendByte appends the string form of the byte count i, as generated by FormatByte, to dst and returns the extended buffer.
func AppendUnicodeBar ¶
func AssertNonZero ¶ added in v5.1.1
func AssertNonZero[T comparable](v T) T
AssertNonZero panics if v is a zero value. Otherwise, it returns the value.
func AssertNotNil ¶ added in v5.1.1
func AssertNotNil[T any](v *T) *T
AssertNotNil panics if v is nil. Otherwise, it returns the value.
func AssertPositive ¶ added in v5.1.1
func AssertPositive[T Number](v T) T
AssertPositive panics if v is not positive. Otherwise, it returns the value.
func BarChart ¶
BarChart prints a bar chart with unicode block elements to help visualize m in a compact way. If maxItems > -1, it limits the amount of lines to display. Example with the number of online players for 8 games (data from steamcharts):
100% 2405676 Total (8 entries) 50% 1195540 Counter-Strike: Global Offensive [██████ ] 20% 473708 Dota 2 [██▍ ] 11% 275232 Apex Legends [█▍ ] 10% 246234 PUBG: BATTLEGROUNDS [█▎ ] 5% 123765 Grand Theft Auto V [▋ ] 3% 64405 Team Fortress 2 [▍ ] 1% 21228 The Sims™ 4 [▏ ] 0% 5564 Sekiro™: Shadows Die Twice [ ]
func Check ¶
func Check(err error)
Check panics if its argument is a non-nil error. Examples:
Check(os.Chdir("directory"))
Check(json.NewDecoder(os.Stdin).Decode(&data))
func Close ¶ added in v5.4.0
Close is a shortcut, instead of writing:
defer func() { Check(f.Close()) }()
One can write:
defer Close(f)
If it is called more than once, it is a no-op
func CloseAfterRead ¶
func CloseAfterRead(r io.ReadCloser) io.Reader
CloseAfterRead returns a Reader that automatically closes when there is no more data to read or an error has occurred. Examples:
// Prints SHA2 of "file_to_hash" in hexadecimal notation
h := sha256.New()
C2(io.Copy(h, CloseAfterRead(C2(os.Open("file_to_hash")))))
fmt.Println(hex.EncodeToString(h.Sum(nil)))
// Downloads a file
const url = "https://go.dev/dl/go1.20.2.linux-amd64.tar.gz"
dst := C2(os.Create(path.Base(url)))
defer Closing(dst)
C2(io.Copy(dst, CloseAfterRead(C2(http.Get(url)).Body)))
func CopyDir ¶ added in v5.2.0
CopyDir copies the contents of src to dst This function will be deprecated once os.CopyFS works with symlinks:
os.CopyFS(dst, os.DirFS(src))
func DebugInfos ¶ added in v5.4.4
func DebugInfos() string
func FormatByte ¶
func Goroutines ¶
Goroutines spawns nb goroutines executing fn. It returns a function that waits for them to finish.
func Has ¶
func Has[M ~map[K]V, K comparable, V any](m M, k K) bool
Has returns whether k is present in the map m.
func InitUnicodeCategory ¶
func InitUnicodeCategory()
InitUnicodeCategory inits the cache for UnicodeCategory It is exported so that the user can trigger cache building instead of waiting for the first call to UnicodeCategory.
func Intn ¶
func Intn[T Integer](n T) T
Intn returns a uniform random value in [0, n). It panics if n <= 0.
func Keys ¶
func Keys[M ~map[K]V, K comparable, V any](m M) []K
Keys returns the keys of the map m. The keys will be in an indeterminate order.
func MultiLines ¶
MultiLines formats a multiline raw string, changing:
` First line Second line Third line `
to:
`First line Second line Third line`
It is intended to be called like this:
MultiLines(` First Line Second line Third line `)
func Must ¶
Must panics if its second argument is a non-nil error and returns the first one. Examples:
i := Must(strconv.Atoi("123"))
f := Must(os.Open("file"))
func Must2 ¶
Must2 panics if its third argument is a non-nil error and returns the first two. Examples:
img, _ := Must2(image.Decode(f)) _, port := Must2(net.SplitHostPort(address))
func One ¶
func One[M ~map[K]V, K comparable, V any](m M) (K, V)
One returns a key and value from a map, which must not be empty.
func Output ¶
Output is similar to (*exec.Cmd).Output() but conveniently wraps exec.ExitError with stderr:
_, err1 := exec.Command("ls", "").Output() // standard way
_, err2 := Output(exec.Command("ls", "")) // with this function
err1.Error() == "exit status 2"
err2.Error() == "exit status 2: ls: cannot access '': No such file or directory"
The underlying *exec.ExitError remains recoverable:
if err := new(exec.ExitError); errors.As(err2, &err) {
fmt.Println(err.UserTime())
}
func RandomBytes ¶
func Run ¶
Run is similar to (*exec.Cmd).Run() but conveniently wraps exec.ExitError with stdout & stderr:
err1 := exec.Command("go", "run").Run() // standard way
err2 := Run(exec.Command("go", "run")) // with this function
err1.Error() == "exit status 1"
err2.Error() == "exit status 1: go: no go files listed"
The underlying *exec.ExitError remains recoverable:
if err := new(exec.ExitError); errors.As(err2, &err) {
fmt.Println(err.UserTime())
}
func Set ¶
func Set[M ~map[K]V, K comparable, V any](m M, k K, v V) bool
func StdinIsPipe ¶
func StdinIsPipe() bool
func StdoutIsTerminal ¶
func StdoutIsTerminal() bool
func ToSet ¶
func ToSet[S ~[]E, E comparable](s S) map[E]struct{}
func UnicodeBar ¶
func UnicodeCategory ¶
UnicodeCategory returns the code point General Category. It is a two bytes string with major & minor e.g. "Lu", "Zs", "Nd"...
func Values ¶
func Values[M ~map[K]V, K comparable, V any](m M) []V
Values returns the values of the map m. The values will be in an indeterminate order.