Created
December 29, 2015 18:12
-
-
Save rfguri/1af5a2c305b14383d1ef to your computer and use it in GitHub Desktop.
One second interval task scheduler that quits on enter key press
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 ( | |
"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