github.com/hongwozai/go-src-1.4.3@v0.0.0-20191127132709-dc3fce3dbccb/doc/progs/timeout1.go (about) 1 // compile 2 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() { 16 time.Sleep(1 * time.Second) 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 }