This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type ValSet struct { | |
one, two, three, four *int | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type fber func(int)string | |
func fizzer(i int) string { | |
return "fizz" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package future | |
// Interface represents a future. No concrete implementation is | |
// exposed; all access to a future is via this interface. | |
type Interface interface { | |
// Get returns the values calculated by the future. It will pause until | |
// the value is calculated. | |
// | |
// If Get is invoked multiple times, the same value will be returned each time. | |
// Subsequent calls to Get will return instantaneously. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Process func() (interface{}, error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func New(inFunc Process) Interface { | |
//… | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"time" | |
"fmt" | |
"future" | |
) | |
func doSomethingThatTakesAWhile(i int) (int, error) { | |
// what this does isn’t that important for the example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type futureImpl struct { | |
done chan struct{} | |
val interface{} | |
err error | |
} | |
func (f *futureImpl) Get() (interface{}, error) { | |
<-f.done | |
return f.val, f.err | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// New creates a new Future that wraps the provided function. | |
func New(inFunc Processor) Interface { | |
f := &futureImpl { | |
done: make(chan struct{}), | |
} | |
go func() { | |
f.val, f.err = inFunc() | |
close(f.done) | |
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Interface represents a future. No concrete implementation is | |
// exposed; all access to a future is via this interface. | |
type Interface interface { | |
// Get returns the values calculated by the future. It will pause until | |
// the value is calculated. | |
// | |
// If Get is invoked multiple times, the same value will be returned each time. | |
// Subsequent calls to Get will return instantaneously. | |
Get() (interface{}, error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (f *futureImpl) GetUntil(d time.Duration) (interface{}, bool, error) { | |
select { | |
case <-f.done: | |
val, err := f.Get() | |
return val, false, err | |
case <-time.After(d): | |
return nil, true, nil | |
} | |
// This should never be executed |