Skip to content

Instantly share code, notes, and snippets.

@innermond
Created January 4, 2018 15:57
Show Gist options
  • Save innermond/9aadae1cf5007f372bc082f6035df6fa to your computer and use it in GitHub Desktop.
Save innermond/9aadae1cf5007f372bc082f6035df6fa to your computer and use it in GitHub Desktop.
timeout a forever cycle with select
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