Skip to content

Instantly share code, notes, and snippets.

@esimov

esimov/goroutine_timer.go

Last active Jan 29, 2016
Embed
What would you like to do?
Timer goroutine pattern
package main
import (
"fmt"
"time"
)
func main() {
for i := 0; i < 10; i++ {
ch := timer(1 * time.Second)
currentTime := <-ch
fmt.Println(currentTime.Format("Mon Jan _2 15:04:05 2006"))
}
}
func timer(t time.Duration)<-chan time.Time{
ch := make(chan time.Time)
go func() {
time.Sleep(t)
ch <- time.Now()
}()
return ch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment