Categorygithub.com/unixpickle/essentials
modulepackage
1.3.0
Repository: https://github.com/unixpickle/essentials.git
Documentation: pkg.go.dev

# README

essentials

There are some simple things that should be built-in to Go. As I find such things, I'll add them to this package. See the GoDoc for more. I will try to document some of the package's functionality here in the README, but not everything.

Slice deletion

Deleting elements from slices in Go is annoying and error prone. With essentials, the hard work is done for you:

mySlice := []int{1, 2, 3, 4}
essentials.OrderedDelete(&mySlice, 2)
fmt.Println(mySlice) // [1 2 4]

If you don't care about the order of your slice, then essentials.UnorderedDelete is faster and will also do the job.

The Die API

This API is useful for CLI apps where you want to exit with an error code in several places. Take this for example:

if dataFile == "" {
	fmt.Fprintln(os.Stderr, "Missing -data flag. See -help for more info.")
	os.Exit(1)
}

log.Println("Loading encoder...")
var enc tweetenc.Encoder
if err := serializer.LoadAny(encFile, &enc.Block); err != nil {
	fmt.Fprintln(os.Stderr, "Load encoder:", err)
	os.Exit(1)
}

dataReader, err := os.Open(dataFile)
if err != nil {
	fmt.Fprintln(os.Stderr, "Open data:", err)
	os.Exit(1)
}

In three different places, I print to standard error and then exit with a status code. It would be so much less typing to do this:

if dataFile == "" {
	essentials.Die("Missing -data flag. See -help for more info.")
}

log.Println("Loading encoder...")
var enc tweetenc.Encoder
if err := serializer.LoadAny(encFile, &enc.Block); err != nil {
	essentials.Die("Load encoder:", err)
}

dataReader, err := os.Open(dataFile)
if err != nil {
	essentials.Die("Open data:", err)
}

# Functions

AbsInt computes the absolute value of an int.
AddCtx creates a *CtxError by adding some context to an existing error.
AddCtxTo is like doing *err = AddCtx(ctx, *err) It is useful for adding context to a named return argument, like in: func MyMethod() (err error) { defer essentials.AddCtxTo("MyMethod", &err) // Code here..
ConcurrentMap calls f for every integer in [0, n).
Contains checks if a slice contains a value.
Die prints the arguments to standard error in a style like the one used by fmt.Println, then exits with an error status code.
MaxInt computes the maximum of the arguments.
MinInt computes the minimum of the arguments.
Must dies with the error if it is non-nil.
OrderedDelete removes the indexed element from the slice and moves the following elements to fill its place.
ReduceConcurrentMap is like StatefulConcurrentMap, but a final reduction function is called at the end of each Goroutine.
Reverse reverses a slice in-place.
Round rounds to the nearest whole number.
StatefulConcurrentMap is like ConcurrentMap, but it calls g once per Goroutine, and then calls the result of g with every index on that Goroutine.
UnorderedDelete removes the indexed element from the slice, potentially changing the order of the slice in the process.
VoodooSort sorts the list using the comparator, while simultaneously re-ordering a set of other lists to match the re-ordering of the sorted list.

# Structs

CtxError is an error with some added context.