Created
August 25, 2014 15:25
-
-
Save Imater/8c882bb6327852162a6a to your computer and use it in GitHub Desktop.
timer with select case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "time" | |
import "fmt" | |
func main() { | |
doneChan := make(chan bool) | |
go func() { | |
time.Sleep(time.Second * 20) | |
doneChan <- true | |
}() | |
for { | |
select { | |
case <- time.NewTimer(time.Second*10).C: | |
fmt.Println("Timer expired") | |
case <- time.NewTicker(time.Millisecond * 400).C: | |
fmt.Println("Ticker ticked") | |
case <- doneChan: | |
fmt.Println("Done") | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment