Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import "time"
- import "fmt"
- var (
- timeout int = 3
- job int = 2
- )
- func main() {
- jobTime := time.Second * time.Duration(job)
- timeoutTime := time.Second * time.Duration(timeout)
- fmt.Printf("jobTime[%v][%T]\n", jobTime, jobTime)
- fmt.Printf("timeoutTime[%v][%T]\n", timeoutTime, timeoutTime)
- c1 := make(chan string, 1)
- go func() {
- // do some work ....
- time.Sleep(jobTime)
- c1 <- "result - job has finished in time"
- }()
- select {
- case res := <-c1:
- fmt.Println(res)
- case <-time.After(timeoutTime):
- fmt.Println("job has been timed out !!")
- }
- }
Add Comment
Please, Sign In to add comment