github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/context/withtimeout_test.go (about) 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. 4 5 package context_test 6 7 import ( 8 "context" 9 "fmt" 10 "time" 11 ) 12 13 func ExampleWithTimeout() { 14 // Pass a context with a timeout to tell a blocking function that it 15 // should abandon its work after the timeout elapses. 16 ctx, _ := context.WithTimeout(context.Background(), 50*time.Millisecond) 17 select { 18 case <-time.After(1 * time.Second): 19 fmt.Println("overslept") 20 case <-ctx.Done(): 21 fmt.Println(ctx.Err()) // prints "context deadline exceeded" 22 } 23 // Output: 24 // context deadline exceeded 25 }