Skip to content

Instantly share code, notes, and snippets.

@sdhjl2000
Created May 31, 2015 14:57
Show Gist options
  • Save sdhjl2000/059f649b2c34bacf8ebf to your computer and use it in GitHub Desktop.
Save sdhjl2000/059f649b2c34bacf8ebf to your computer and use it in GitHub Desktop.
return first done or wait all
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