lang

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: BSD-3-Clause Imports: 1 Imported by: 0

README

lang

Package lang provides some helpful language features excluded from the Go language.

Getting Started

The lang package can be added to a Go project with go get.

go get github.com/shoenig/lang@latest
import "github.com/shoenig/lang"
Examples
Critical

Use lang.Critical to hold a mutex lock and execute a function.

lang.Critical(lock, func() {
  shared += 1
})

Use lang.Critical to hold a mutex lock and execute a function with a return value.

v := lang.CriticalGet(lock, func() int {
  shared += 1
  return shared
})
Map

Use lang.Map to apply a function across all elements of a slice.

conversion := lang.Map[int, string](originals, func(i int) string {
  return strconv.Itoa(i)
})
Filter / FilterFunc

Use lang.Filter or lang.FilterFunc to remove elements from a slice.

filtered := lang.Filter[string](items, unwanted...)
filtered := lang.FilterFunc[string](items, func(s string) bool {
  return someCondition(s)
})
Maybe

Use lang.Maybe as the ternary-operator for Go.

passing := lang.Maybe[string](check, "ok", "fail")
Head

Use lang.Head to get a sub-slice of the first N elements of another slice. lang.Head will return up to N elements, gracefully handling slices that may have fewer than N elements.

best := lang.Head[string](items, 3)
Tail

Use lang.Tail to get a sub-slice of the last N elements of another slice. lang.Tail will return up to N elements, gracefully handling slices that may have fewer than N elements.

worst := lang.Tail[string](items, 3)
Pairs

Use lang.Pair to conveniently associate two types together.

p := lang.Pair[string, int]{A: "bob", B: 42}
Contributing

Contributions are welcome! Feel free to help make lang better by filing an issue or opening a PR.

License

The github.com/shoenig/lang module is open source under the BSD-3-Clause license.

Documentation

Overview

Package lang provides some helpful or at least amusing language features not included in the standard Go language or libraries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Critical added in v0.0.8

func Critical(mutex Mutex, f func())

Critical executes f while holding the mutex.

func CriticalGet added in v0.0.8

func CriticalGet[T any](mutex Mutex, f func() T) T

CriticalGet executes f while holding the mutex, and returns the return value of f.

func Filter added in v0.0.7

func Filter[A comparable](input []A, items ...A) []A

Filter removes the list of items from input.

Note that Filter yields a deep copy.

func FilterFunc added in v0.0.7

func FilterFunc[A any](input []A, f func(A) bool) []A

FilterFunc uses f to remove elements from input. If f returns true for an element, that element is not included in the result.

Note that FilterFunc creates a deep copy.

func Head[A any](input []A, count int) []A

Head returns up to the first count elements in the given input slice.

Note that this is yields a shallow copy.

func Map added in v0.0.3

func Map[A, B any](input []A, f func(A) B) []B

Map applies function f to every A element in input to create a new slice of type B.

func Maybe added in v0.0.2

func Maybe[T any](b bool, value T, fallback T) T

Maybe returns value if b is true, or fallback otherwise.

func Tail added in v0.0.6

func Tail[A any](input []A, count int) []A

Tail returns up to the last count elements in the given input slice.

Note that this yields a shallow copy.

Types

type Mutex added in v0.0.8

type Mutex interface {
	Lock()
	Unlock()
}

Mutex is anything that implements Lock and Unlock.

type Pair added in v0.0.1

type Pair[T, U any] struct {
	A T
	B U
}

A Pair contains two elements of any type(s).

Jump to

Keyboard shortcuts

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