Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anandharaj-dotworld/3f969a5e2fe1560037a68c69cf455eb0 to your computer and use it in GitHub Desktop.
Save anandharaj-dotworld/3f969a5e2fe1560037a68c69cf455eb0 to your computer and use it in GitHub Desktop.
Exploring Goroutines and Tickers in Go: A Deep Dive into Concurrency
// Write a sample code for goroutine with ticker
package main
import (
"fmt"
"time"
)
func main() {
newTicker := time.NewTicker(1 * time.Second)
done := make(chan bool)
go func() {
// Write here to run your function
for {
select {
case <-done:
return
case t := <-newTicker.C:
fmt.Println("Ticker at, ", t)
}
}
}()
// Time to close ticker
time.Sleep(10 * time.Second)
newTicker.Stop()
done <- true
// This is important!
fmt.Println("Ticker was now closed")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment