Skip to content

Instantly share code, notes, and snippets.

@wjx0912
Last active March 23, 2018 07:54
Show Gist options
  • Save wjx0912/efb147bc425cb1d2cf33c56900577dac to your computer and use it in GitHub Desktop.
Save wjx0912/efb147bc425cb1d2cf33c56900577dac to your computer and use it in GitHub Desktop.
golang timeout1 (每个子任务超时)
package main
import (
"fmt"
"time"
)
func add(ch chan int) {
for i := 0; i < 10; i++ {
ch <- i
}
}
func main() {
ch := make(chan int, 10)
go add(ch)
for {
select {
case <-time.After(2 * time.Second):
fmt.Println("timeout")
return
case <-ch:
fmt.Println("sleep one seconds ...")
time.Sleep(1 * time.Second)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment