Skip to content

Instantly share code, notes, and snippets.

@wjx0912
Created March 23, 2018 07:54
Show Gist options
  • Save wjx0912/3d11fea0079fcac86b5464e702d7d249 to your computer and use it in GitHub Desktop.
Save wjx0912/3d11fea0079fcac86b5464e702d7d249 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() {
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
ch := make(chan int, 10)
go add(ch)
for {
select {
case <- ch:
time.Sleep(1 * time.Second)
fmt.Println("sleep one seconds ...")
case <- ticker.C:
fmt.Println("timeout")
return
default:
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment