Shenghou Ma | 5b7562d | 2012-09-03 03:49:03 +0800 | [diff] [blame] | 1 | // compile |
| 2 | |
Francisco Souza | 5659826 | 2012-03-14 13:03:11 +1100 | [diff] [blame] | 3 | // Copyright 2012 The Go Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | package timeout |
| 7 | |
| 8 | import ( |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | func Timeout() { |
| 13 | ch := make(chan bool, 1) |
| 14 | timeout := make(chan bool, 1) |
| 15 | go func() { |
Rob Pike | a99e9c5 | 2012-03-23 07:51:16 +1100 | [diff] [blame] | 16 | time.Sleep(1 * time.Second) |
Francisco Souza | 5659826 | 2012-03-14 13:03:11 +1100 | [diff] [blame] | 17 | timeout <- true |
| 18 | }() |
| 19 | |
| 20 | // STOP OMIT |
| 21 | |
| 22 | select { |
| 23 | case <-ch: |
| 24 | // a read from ch has occurred |
| 25 | case <-timeout: |
| 26 | // the read from ch has timed out |
| 27 | } |
| 28 | |
| 29 | // STOP OMIT |
| 30 | } |