Last active
March 19, 2018 19:10
-
-
Save jomoespe/066191b69c64d871b7a1cb7d4ed45cca to your computer and use it in GitHub Desktop.
How to run a gorutine repetitive.
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
// 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