Last active
January 29, 2016 13:51
-
-
Save esimov/f30d93d51712f2f85f49 to your computer and use it in GitHub Desktop.
Timer goroutine pattern
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
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