Last active
December 10, 2015 01:58
-
-
Save hugozhu/4364389 to your computer and use it in GitHub Desktop.
Use go to implement a simple workflow
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 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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a在5s内保证返回这个,是不是缺了一个select呢?