Skip to content

Instantly share code, notes, and snippets.

@jomoespe
Last active March 19, 2018 19:10
Show Gist options
  • Save jomoespe/066191b69c64d871b7a1cb7d4ed45cca to your computer and use it in GitHub Desktop.
Save jomoespe/066191b69c64d871b7a1cb7d4ed45cca to your computer and use it in GitHub Desktop.
How to run a gorutine repetitive.
// Based on http://stackoverflow.com/questions/16466320/is-there-a-way-to-do-repetitive-tasks-at-intervals-in-golang
// Probably need to read more if some mutex if gotunite needs to access shared information.
package main
import (
"log"
"time"
)
const (
duration = 30 * time.Second
interval_time = 5 * time.Second
)
func main() {
ticker := time.NewTicker(interval_time)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
log.Println("\tdoing somethig.....")
case <-quit:
ticker.Stop()
return
}
}
}()
log.Printf("Repeating a task each %v during %v.", interval_time, duration)
<-time.After(time.Duration(duration))
log.Println("End")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment