github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/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 "fmt" 9 "time" 10 11 "golang.org/x/net/context" 12 ) 13 14 func ExampleWithTimeout() { 15 // Pass a context with a timeout to tell a blocking function that it 16 // should abandon its work after the timeout elapses. 17 ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond) 18 select { 19 case <-time.After(200 * time.Millisecond): 20 fmt.Println("overslept") 21 case <-ctx.Done(): 22 fmt.Println(ctx.Err()) // prints "context deadline exceeded" 23 } 24 // Output: 25 // context deadline exceeded 26 }