futurevalue

package
v1.0.0-beta1 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2019 License: Apache-2.0 Imports: 4 Imported by: 22 modules, 21 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Initializer

type Initializer func() (interface{}, error)

Initializer initializes the value

type Value

type Value struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Value implements a Future Value in which a reference is initialized once (and only once) using the Initialize function. Only one Go routine can call Initialize whereas multiple Go routines may invoke Get, and will wait until the reference has been initialized. Regardless of whether Initialize returns success or error, the value cannot be initialized again.

func New

func New(initializer Initializer) *Value

New returns a new future value

func (*Value) Get

func (f *Value) Get() (interface{}, error)

Get returns the value and/or error that occurred during initialization.

Example
fv := New(func() (interface{}, error) {
	return "Value1", nil
})

done := make(chan bool)
go func() {
	value, err := fv.Get()
	if err != nil {
		fmt.Printf("Error returned from Get: %s\n", err)
	}
	fmt.Println(value)
	done <- true
}()

fv.Initialize()
<-done
Output:
Value1

func (*Value) Initialize

func (f *Value) Initialize() (interface{}, error)

Initialize initializes the future value. This function must be called only once. Subsequent calls may result in deadlock.

func (*Value) IsSet

func (f *Value) IsSet() bool

IsSet returns true if the value has been set, otherwise false is returned

func (*Value) MustGet

func (f *Value) MustGet() interface{}

MustGet returns the value. If an error resulted during initialization then this function will panic.

Example
fv := New(func() (interface{}, error) {
	return "Value1", nil
})

done := make(chan bool)
go func() {
	fmt.Println(fv.MustGet())
	done <- true
}()

fv.Initialize()
<-done
Output:
Value1

Jump to

Keyboard shortcuts

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