Skip to content

Instantly share code, notes, and snippets.

@hugozhu
Last active December 10, 2015 01:58
Show Gist options
  • Save hugozhu/4364389 to your computer and use it in GitHub Desktop.
Save hugozhu/4364389 to your computer and use it in GitHub Desktop.
Use go to implement a simple workflow
func callA() string {
time.Sleep(time.Millisecond * 300)
return "Hello A"
}
func callB() string {
time.Sleep(time.Millisecond * 100)
return "Hello B"
}
func callC() string {
time.Sleep(time.Millisecond * 200)
return "Hello C"
}
func main() {
a_is_done := make(chan bool)
b_is_done := make(chan bool)
go func() {
log.Println(callA())
a_is_done <- true
}()
go func() {
log.Println(callB())
go func() {
log.Println(callC())
}()
b_is_done <- true
}()
t := time.Now()
<-a_is_done //wait for A to finish or timeout
select {
case <-b_is_done:
case <-time.After(time.Duration(time.Millisecond*200) - time.Now().Sub(t)): //todo: duration <= 0
}
}
@spin6lock
Copy link

a在5s内保证返回这个,是不是缺了一个select呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment