Created
January 4, 2018 15:57
-
-
Save innermond/9aadae1cf5007f372bc082f6035df6fa to your computer and use it in GitHub Desktop.
timeout a forever cycle with select
This file contains hidden or 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 ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
func main() { | |
t := time.Now().Unix() | |
rand.Seed(t) | |
timeout := time.After(1 * time.Second) | |
Loop: | |
for { | |
select { | |
case <-timeout: | |
break Loop | |
default: | |
i := rand.Intn(100) | |
fmt.Println(t, ": ", i) | |
} | |
} | |
fmt.Println("End of anything\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment