func main() { ticker := time.NewTicker(500 * time.Millisecond) done := make(chan bool) go func() { for { select { case <-done: return case t := <-ticker.C: fmt.Println("Tick at", t) } } }() time.Sleep(1600 * time.Millisecond) ticker.Stop() done <- true fmt.Println("Ticker stopped") }
func main() { ticker := time.NewTicker(1 * time.Second) counter := 0 for { select { case <-ticker.C: counter++ fmt.Println("Counter:", counter) if counter >= 5 { ticker.Stop() fmt.Println("Ticker stopped") return } } } }In conclusion, the go time Ticker is used to create periodic events and is part of the time package library. It can be useful in various scenarios such as polling, syncing data, etc.