fabric-sdk-go: github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/futurevalue Index | Examples | Files

package futurevalue

import "github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/futurevalue"

Index

Examples

Package Files

futurevalue.go

type Initializer Uses

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

Initializer initializes the value

type Value Uses

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 Uses

func New(initializer Initializer) *Value

New returns a new future value

func (*Value) Get Uses

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

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

Code:

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 Uses

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 Uses

func (f *Value) IsSet() bool

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

func (*Value) MustGet Uses

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

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

Code:

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

Package futurevalue imports 4 packages (graph) and is imported by 1 packages. Updated 2018-07-18. Refresh now. Tools for package owners.