Created
May 31, 2015 14:57
-
-
Save sdhjl2000/059f649b2c34bacf8ebf to your computer and use it in GitHub Desktop.
return first done or wait all
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
package main | |
import "fmt" | |
import ( | |
"time" | |
) | |
//import "math/rand" | |
func main() { | |
count := 3 | |
result := make(chan int) | |
done := make(chan bool) | |
go func() { // We launch the goroutine from inside the function. | |
time.Sleep(time.Duration(3) * time.Second) | |
result <- 3 | |
}() | |
go func() { // We launch the goroutine from inside the function. | |
time.Sleep(time.Duration(2) * time.Second) | |
result <- 2 | |
done <- true | |
}() | |
go func() { // We launch the goroutine from inside the function. | |
time.Sleep(time.Duration(1) * time.Second) | |
result <- 1 | |
}() | |
//timeout := time.After(3 * time.Second) | |
for { | |
select { | |
case s := <-result: | |
count = count - 1 | |
fmt.Println(s) | |
if count == 0 { | |
fmt.Println("finish" + string(s)) | |
return | |
} | |
case <-done: //和上次消息的差别 | |
fmt.Println("done") | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment