timeout1.go 481 B

1234567891011121314151617181920212223242526272829
  1. // Copyright 2012 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 timeout
  5. import (
  6. "time"
  7. )
  8. func Timeout() {
  9. ch := make(chan bool, 1)
  10. timeout := make(chan bool, 1)
  11. go func() {
  12. time.Sleep(1 * time.Second)
  13. timeout <- true
  14. }()
  15. // STOP OMIT
  16. select {
  17. case <-ch:
  18. // a read from ch has occurred
  19. case <-timeout:
  20. // the read from ch has timed out
  21. }
  22. // STOP OMIT
  23. }