types

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2023 License: MIT Imports: 3 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 Some

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

Some returns an Option that contains a 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]) String

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

func (*Option[T]) UnmarshalJSON

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

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

Jump to

Keyboard shortcuts

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