generic

package module
v0.0.0-...-32842ed Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2023 License: Apache-2.0 Imports: 0 Imported by: 0

README

helper

A collection of small helper functions which use generics

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert[T any](v any) (asserted T, ok bool)

Assert does type assertion on v. v is asserted against T and *T. Assert returns true ok when v is either of T or *T. If v is nil of *T, then asserted is zero value of T.

func Escape

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

Escape escapes v to a pointer of v.

It is useful when setting built-in type T (e.g. string, int) to struct fields of *T.

func ExtractError

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

func ExtractError3

func ExtractError3[T, U any](v1 T, v2 U, err error) error

func Must

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

Must unwraps error possibility from a set of return value from a function. Must panics if err is non nil error. The main use case is initialization of package-top level variables.

func initializeSomeValue() (int, error) {
	return 0, errors.New("foo")
}

var SomeValue = Must(initializeSomeValue()) // this line panics, meaning incorrect implementation.

This example omits use of the init() function.

func Must3

func Must3[T any, U any](val1 T, val2 U, err error) (T, U)

Must3 is same as Must but takes 3 args.

It panics if err is non nil.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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