Skip to content

Instantly share code, notes, and snippets.

@foolishway
Created August 12, 2020 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foolishway/1ee61c6359b04be8f35c724572845413 to your computer and use it in GitHub Desktop.
Save foolishway/1ee61c6359b04be8f35c724572845413 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
)
type Future struct {
C chan struct{}
}
func NewFuture() *Future {
return &Future{ make(chan struct{}) }
}
func (f *Future) Wait() {
<- f.C
}
func (f *Future) Resolve() {
f.C <- struct{}{}
}
func main() {
var value string
f := NewFuture()
go func() {
time.Sleep(10 * time.Second)
fmt.Println("10 seconds later")
value = "hello world"
f.Resolve()
}()
f.Wait()
fmt.Println(value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment