Created
August 12, 2020 02:02
-
-
Save foolishway/1ee61c6359b04be8f35c724572845413 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
"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