Skip to content

Instantly share code, notes, and snippets.

@esimov
Last active January 29, 2016 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esimov/f30d93d51712f2f85f49 to your computer and use it in GitHub Desktop.
Save esimov/f30d93d51712f2f85f49 to your computer and use it in GitHub Desktop.
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