Skip to content

Instantly share code, notes, and snippets.

@cvanderschuere
Created May 14, 2015 01:42
Show Gist options
  • Save cvanderschuere/c454fa4ebd656f0aaf74 to your computer and use it in GitHub Desktop.
Save cvanderschuere/c454fa4ebd656f0aaf74 to your computer and use it in GitHub Desktop.
Go Overview
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