once

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2020 License: BSD-3-Clause Imports: 2 Imported by: 3

README

hsson/once

PkgGoDev GoReportCard

A re-implementation and drop-in replacement of the standard sync.Once, with added support for return values! This package exports three additional Once-like primitives, in addition to the standard once.Once:

once.Error returns an error value

Do(f func() error) error

once.Value returns a value

Do(f func() interface{}) interface{}

once.ValueError returns a (value, error) tuple

Do(f func() (interface{}, error)) (interface{}, error)

These three primitives have the behavior that, like with the standard Once, the function passed is ever only executed once. However, they also return the value returned by that one execution to all subsequent callers.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error added in v1.0.1

type Error struct {
	// contains filtered or unexported fields
}

Error is similar to Once, except it returns an error value.

func (*Error) Do added in v1.0.1

func (o *Error) Do(f func() error) error

Do runs the specified function only once, but all callers gets the same result from that single execution.

type Once

type Once struct {
	// contains filtered or unexported fields
}

Once is backwards compatible re-implementation of sync.Once. See https://golang.org/pkg/sync/#Once

func (*Once) Do

func (o *Once) Do(f func())

Do is a backwards compatible re-implementation of Do from sync.Once

type Value added in v1.0.1

type Value struct {
	// contains filtered or unexported fields
}

Value is similar to Once, except it returns a value.

func (*Value) Do added in v1.0.1

func (o *Value) Do(f func() interface{}) interface{}

Do runs the specified function only once, but all callers gets the same result from that single execution.

type ValueError added in v1.0.1

type ValueError struct {
	// contains filtered or unexported fields
}

ValueError is similar to Once, except it return a (value, error) tuple

func (*ValueError) Do added in v1.0.1

func (o *ValueError) Do(f func() (interface{}, error)) (interface{}, error)

Do runs the specified function only once, but all callers gets the same result from that single execution.

Jump to

Keyboard shortcuts

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