1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 "golang.org/x/net/context"
14 // This example passes a context with a timeout to tell a blocking function that
15 // it should abandon its work after the timeout elapses.
16 func ExampleWithTimeout() {
17 // Pass a context with a timeout to tell a blocking function that it
18 // should abandon its work after the timeout elapses.
19 ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
23 case <-time.After(1 * time.Second):
24 fmt.Println("overslept")
26 fmt.Println(ctx.Err()) // prints "context deadline exceeded"
30 // context deadline exceeded