package main import ( "fmt" "time" ) func main() { counter := 0 ticker := time.NewTicker(time.Second) defer ticker.Stop() for { select { case <-ticker.C: counter++ fmt.Println(counter) if counter >= 10 { return } } } }
package main import ( "fmt" "net/http" "time" ) func serverTimeHandler(w http.ResponseWriter, r *http.Request) { ticker := time.NewTicker(time.Second * 5) defer ticker.Stop() for { select { case <-ticker.C: fmt.Fprintf(w, "Current time: %v\n", time.Now().Format("2006-01-02 15:04:05")) w.(http.Flusher).Flush() case <-r.Context().Done(): return } } } func main() { http.HandleFunc("/servertime", serverTimeHandler) http.ListenAndServe(":8080", nil) }In this example, an HTTP endpoint `/servertime` is created that sends periodic updates of the current time to the client every 5 seconds. The `ticker` is stopped when the client disconnects from the HTTP connection. The `time` package is a built-in package in Go and does not require any additional package imports.