gog

package
v1.2.11 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 0 Imported by: 10

Documentation

Overview

Package gog contains general, generic extensions to the Go language, requiring generics (introduced in Go 1.18).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func First

func First[T any](first T, _ ...any) T

First returns the first argument. Useful when you want to use the first result of a function call that has more than one return values (e.g. in a composite literal or in a condition).

For example:

func f() (i, j, k int, s string, f float64) { return }

p := image.Point{
    X: First(f()),
}

func If

func If[T any](cond bool, vtrue, vfalse T) T

If returns vtrue if cond is true, vfalse otherwise.

Useful to avoid an if statement when initializing variables, for example:

n := If(i > 0, i, 0)

func Must

func Must[T any](v T, err error) T

Must takes 2 arguments, the second being an error. If err is not nil, Must panics. Else the first argument is returned.

Useful when inputs to some function are provided in the source code, and you are sure they are valid (if not, it's OK to panic). For example:

t := Must(time.Parse("2006-01-02", "2022-04-20"))

func Ptr

func Ptr[T any](v T) *T

Ptr returns a pointer to the passed value.

Useful when you have a value and need a pointer, e.g.:

func f() string { return "foo" }

foo := struct{
    Bar *string
}{
    Bar: Ptr(f()),
}

func Second

func Second[T any](_ any, second T, _ ...any) T

Second returns the second argument. Useful when you want to use the second result of a function call that has more than one return values (e.g. in a composite literal or in a condition).

For example:

func f() (i, j, k int, s string, f float64) { return }

p := image.Point{
    X: Second(f()),
}

func Third

func Third[T any](_, _ any, third T, _ ...any) T

Third returns the third argument. Useful when you want to use the third result of a function call that has more than one return values (e.g. in a composite literal or in a condition).

For example:

func f() (i, j, k int, s string, f float64) { return }

p := image.Point{
    X: Third(f()),
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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