types

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: MIT Imports: 7 Imported by: 8

README

Useful generic types for Go

PkgGoDev GHA Build Slack chat

I've found myself reimplementing some small types and patterns in a lot of my recent Go projects, so I thought I'd distill them into a single package.

Refer to the package docs for details.

Documentation

Overview

Package types contains a collection of types and functions that are useful in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go

func Go[T any](f func() (T, error)) chan Result[T]

Go runs a function in a goroutine and returns a channel that will receive the Result.

Types

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}

An Option type is a type that can contain a value or nothing.

func None

func None[T any]() Option[T]

None returns an Option that contains nothing.

func Ptr added in v0.5.0

func Ptr[T any](ptr *T) Option[T]

Ptr returns an Option that returns None[T]() if the pointer is nil, otherwise the dereferenced pointer.

func Some

func Some[T any](value T) Option[T]

Some returns an Option that contains a value.

func Zero added in v0.8.0

func Zero[T comparable](value T) Option[T]

Zero returns an Option that returns None[T]() if the value is the zero value, otherwise the value.

func (Option[T]) Default

func (o Option[T]) Default(value T) T

Default returns the Option value if it is present, otherwise it returns the value passed.

func (Option[T]) Get

func (o Option[T]) Get() (T, bool)

Get returns the value and a boolean indicating if the Option contains a value.

func (Option[T]) GoString

func (o Option[T]) GoString() string

func (Option[T]) MarshalJSON

func (o Option[T]) MarshalJSON() ([]byte, error)

func (Option[T]) MustGet added in v0.7.0

func (o Option[T]) MustGet() T

MustGet returns the value. It panics if the Option contains nothing.

func (Option[T]) Ok added in v0.3.0

func (o Option[T]) Ok() bool

Ok returns true if the Option contains a value.

func (Option[T]) Ptr added in v0.6.0

func (o Option[T]) Ptr() *T

Ptr returns a pointer to the value if the Option contains a value, otherwise nil.

func (*Option[T]) Scan added in v0.7.0

func (o *Option[T]) Scan(src any) error

func (Option[T]) String

func (o Option[T]) String() string

func (*Option[T]) UnmarshalJSON

func (o *Option[T]) UnmarshalJSON(data []byte) error

func (Option[T]) Value added in v0.4.0

func (o Option[T]) Value() (driver.Value, error)

type Pair added in v0.2.0

type Pair[T, U any] struct {
	First  T
	Second U
}

func PairOf added in v0.2.0

func PairOf[T, U any](first T, second U) Pair[T, U]

func (Pair[T, U]) Get added in v0.2.0

func (p Pair[T, U]) Get() (T, U)

type Result

type Result[T any] struct {
	// contains filtered or unexported fields
}

A Result type is a type that can contain an error or a value.

func Err

func Err[T any](err error) Result[T]

Err returns a Result that contains an error.

func Errorf

func Errorf[T any](format string, args ...interface{}) Result[T]

Errorf returns a Result that contains a formatted error.

func Ok

func Ok[T any](value T) Result[T]

Ok returns a Result that contains a value.

func Outcome

func Outcome[T any](value T, err error) Result[T]

Outcome returns a Result that contains a value or an error.

It can be used to convert a function that returns a value and an error into a Result.

func (Result[T]) Default

func (r Result[T]) Default(value T) T

Default returns the Result value if it is present, otherwise it returns the value passed.

func (Result[T]) Err

func (r Result[T]) Err() error

Err returns the error, if any.

func (Result[T]) Get

func (r Result[T]) Get() (T, bool)

Get returns the value and a boolean indicating whether the value is present.

func (Result[T]) GoString

func (r Result[T]) GoString() string

func (Result[T]) MarshalJSON

func (r Result[T]) MarshalJSON() ([]byte, error)

func (Result[T]) String

func (r Result[T]) String() string

func (*Result[T]) UnmarshalJSON

func (r *Result[T]) UnmarshalJSON(data []byte) error

type Triple added in v0.2.0

type Triple[T, U, V any] struct {
	First  T
	Second U
	Third  V
}

func TripleOf added in v0.2.0

func TripleOf[T, U, V any](first T, second U, third V) Triple[T, U, V]

func (Triple[T, U, V]) Get added in v0.2.0

func (t Triple[T, U, V]) Get() (T, U, V)

Jump to

Keyboard shortcuts

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