You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

27 lines
677 B

  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. package context_test
  5. import (
  6. "fmt"
  7. "time"
  8. "golang.org/x/net/context"
  9. )
  10. func ExampleWithTimeout() {
  11. // Pass a context with a timeout to tell a blocking function that it
  12. // should abandon its work after the timeout elapses.
  13. ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
  14. select {
  15. case <-time.After(200 * time.Millisecond):
  16. fmt.Println("overslept")
  17. case <-ctx.Done():
  18. fmt.Println(ctx.Err()) // prints "context deadline exceeded"
  19. }
  20. // Output:
  21. // context deadline exceeded
  22. }