Created
May 14, 2015 01:42
-
-
Save cvanderschuere/c454fa4ebd656f0aaf74 to your computer and use it in GitHub Desktop.
Go Overview
This file contains 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" | |
import "time" | |
func main() { | |
finished := make(chan bool, 1) | |
go ticker(finished) | |
fmt.Println("Hello, playground") | |
<-finished | |
fmt.Println("Finished") | |
} | |
func ticker(finished chan bool){ | |
fmt.Println("Waiting") | |
t := makeTicker(1*time.Second) | |
for i := 0; i < 12; i++{ | |
<-t // receive time every 1 second | |
fmt.Println(i+1,"second(s) passed") | |
} | |
finished<-true | |
} | |
func makeTicker(dur time.Duration)chan time.Time{ | |
tickChan := make(chan time.Time) | |
go func(){ | |
for{ | |
lastTime := time.Now() | |
tickTime := lastTime.Add(dur) | |
for time.Now().Before(tickTime){ | |
// just waiting for time to pass | |
} | |
tickChan<-time.Now() | |
} | |
}() | |
return tickChan | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment