github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/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  }