Golang-超時機制Timeout和心跳HeartBeart--demo


1、超時機制Timeout

package mian

func workerTimeout(start chan bool) {
	timeout := time.After(10 * time.Second)
	for {
		select {
				// … do some stuff
		case <- timeout:
			return
		}
	}
}
fumc main() {
    var intChan chan bool
     intChan = make(chan bool, 1)
    workerTimeout(intChan)
}

  

2、心跳HeartBeart 
 
  與timeout實現類似,下面是一個簡單的心跳select實現:
  
package mian

func workerHeartbeat(start chan bool) {
        heartbeat := time.Tick(1 * time.Second)
        for {
                select {
                     // … do some stuff
				case <- heartbeat:
					fmt.Println("*")
                    //… do heartbeat stuff
                }
        }
}

fumc main() {
    var intChan chan bool
     intChan = make(chan bool, 1)
    workerHeartbeat(intChan)
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM