Skip to content

Instantly share code, notes, and snippets.

@rfguri
Created December 29, 2015 18:12
Show Gist options
  • Save rfguri/1af5a2c305b14383d1ef to your computer and use it in GitHub Desktop.
Save rfguri/1af5a2c305b14383d1ef to your computer and use it in GitHub Desktop.
One second interval task scheduler that quits on enter key press
package main
import (
"bufio"
"fmt"
"os"
"time"
)
func schedule(task func(), delay time.Duration) chan bool {
stop := make(chan bool)
go func() {
for {
task()
select {
case <-time.After(delay):
case <-stop:
return
}
}
}()
return stop
}
func ping() {
fmt.Println("Ping!")
}
func main() {
stop := schedule(ping, 1*time.Second)
bufio.NewReader(os.Stdin).ReadBytes('\n')
stop <- true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment