Skip to content

Instantly share code, notes, and snippets.

@Amit-PivotalLabs
Created August 29, 2016 01:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Amit-PivotalLabs/246134b028de544e557315138acf9679 to your computer and use it in GitHub Desktop.
Save Amit-PivotalLabs/246134b028de544e557315138acf9679 to your computer and use it in GitHub Desktop.
Golang scheduling DSL
package main
import . "time"
type Every Duration
type Do func()
func (schedule Every) Do(schedulable func()) {
go func() {
c := NewTicker(Duration(schedule)).C
for {
<-c
go schedulable()
}
}()
}
func (schedulable Do) Every(schedule Duration) {
Every(schedule).Do(schedulable)
}
func Wait() { select {} }
func main() {
Every(Second).Do(func() { println("every second, do this") })
Sleep(2 * Second)
Do(func() { println("do that, every 500 ms") }).Every(500 * Millisecond)
Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment