Guest User

Untitled

a guest
Nov 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. package main
  2.  
  3. import "time"
  4. import "fmt"
  5.  
  6. var (
  7. timeout int = 3
  8. job int = 2
  9. )
  10.  
  11. func main() {
  12.  
  13. jobTime := time.Second * time.Duration(job)
  14. timeoutTime := time.Second * time.Duration(timeout)
  15.  
  16. fmt.Printf("jobTime[%v][%T]\n", jobTime, jobTime)
  17. fmt.Printf("timeoutTime[%v][%T]\n", timeoutTime, timeoutTime)
  18.  
  19. c1 := make(chan string, 1)
  20. go func() {
  21. // do some work ....
  22. time.Sleep(jobTime)
  23. c1 <- "result - job has finished in time"
  24. }()
  25.  
  26. select {
  27. case res := <-c1:
  28. fmt.Println(res)
  29. case <-time.After(timeoutTime):
  30. fmt.Println("job has been timed out !!")
  31. }
  32.  
  33. }
Add Comment
Please, Sign In to add comment