Skip to content

Instantly share code, notes, and snippets.

@hallazzang
Created December 10, 2023 19:11
Show Gist options
  • Save hallazzang/d65e6b4a3f265b639995733b6f943b0a to your computer and use it in GitHub Desktop.
Save hallazzang/d65e6b4a3f265b639995733b6f943b0a to your computer and use it in GitHub Desktop.
Go ticker which runs immediately after creating
func NewTicker(d time.Duration) *time.Ticker {
ticker := time.NewTicker(d)
oc := ticker.C
nc := make(chan time.Time, 1)
go func() {
nc <- time.Now()
for t := range oc {
nc <- t
}
}()
ticker.C = nc
return ticker
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment