
Untitled
By: a guest on
Jul 18th, 2012 | syntax:
None | size: 0.52 KB | hits: 8 | expires: Never
package main
import (
"fmt"
"time"
)
type Future struct {
feed chan interface{}
val interface{}
}
func NewFuture() *Future {
return &Future{feed: make(chan interface{}, 1)}
}
func (f *Future) Set(v interface{}) {
f.feed <- v
close(f.feed)
}
func (f *Future) Get() interface{} {
v, ok := <-f.feed
if ok {
f.val = v
}
return f.val
}
func main() {
f := NewFuture()
go (func() {
time.Sleep(1e9)
f.Set(12)
})()
v := f.Get()
fmt.Println(v)
v = f.Get()
fmt.Println(v)
}