Golang context.WithTimeout Demo
This file contains 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 ( | |
"context" | |
"fmt" | |
"time" | |
) | |
func main() { | |
wait := make(chan bool) | |
ctx, cancel := context.WithTimeout(context.Background(), 3200*time.Millisecond) | |
defer cancel() | |
go func() { | |
select { | |
case <-ctx.Done(): | |
fmt.Println("Timeout:", string(time.Now().Format("05.00"))) | |
err := ctx.Err() | |
if err != nil { | |
fmt.Println("in <-ctx.Done(): ", err) | |
} | |
// main app is blocking, waiting to hear about how this went | |
wait<-true | |
break | |
} | |
}() | |
time.Sleep(2 * time.Second) | |
fmt.Println("first sleep completed, ",string(time.Now().Format("05.00"))) | |
//cancel() | |
time.Sleep(2 * time.Second) | |
fmt.Println("after second sleep done, ", string(time.Now().Format("05.00"))) | |
<-wait // for the goroutine we started earlier | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
play https://play.golang.org/p/uZTPHmdh4L3