Last active
March 23, 2018 07:54
-
-
Save wjx0912/efb147bc425cb1d2cf33c56900577dac to your computer and use it in GitHub Desktop.
golang timeout1 (每个子任务超时)
This file contains hidden or 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" | |
"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