Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

once source pure

Package once provides utilities for one-time execution patterns. It extends the concept of sync.Once with error handl...

Overview

Package once provides utilities for one-time execution patterns. It extends the concept of sync.Once with error handling and panic options.

Variables 1

Functions 1

func New

1func New() *Once
source

New creates a new Once instance

Types 1

type Once

struct
1type Once struct {
2	done    bool
3	err     error
4	paniced bool
5	value   any // stores the result of the execution
6}
source

Once represents a one-time execution guard

Methods on Once

func Do

method on Once
1func (o *Once) Do(fn func())
source

Do executes fn only once and returns nil on subsequent calls

func DoErr

method on Once
1func (o *Once) DoErr(fn func() error) error
source

DoErr executes fn only once and returns the same error on subsequent calls

func DoOrPanic

method on Once
1func (o *Once) DoOrPanic(fn func())
source

DoOrPanic executes fn only once and panics on subsequent calls

func DoValue

method on Once
1func (o *Once) DoValue(fn func() any) any
source

DoValue executes fn only once and returns its value, subsequent calls return the cached value

func DoValueErr

method on Once
1func (o *Once) DoValueErr(fn func() (any, error)) (any, error)
source

DoValueErr executes fn only once and returns its value and error Subsequent calls return the cached value and error

func Error

method on Once
1func (o *Once) Error() error
source

Error returns the error from the last execution if any

func IsDone

method on Once
1func (o *Once) IsDone() bool
source

IsDone returns whether the Once has been executed

func Reset

method on Once
1func (o *Once) Reset()
source

Reset resets the Once instance to its initial state This is mainly useful for testing purposes

func Value

method on Once
1func (o *Once) Value() (any, error)
source

Value returns the stored value and an error if not executed yet

Imports 1

  • errors stdlib

Source Files 3