lang

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: May 26, 2025 License: BSD-3-Clause Imports: 0 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
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.

best := lang.Head(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 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.

Types

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