Home >Backend Development >Golang >How to schedule tasks in golang
In programs, it is often necessary to call functions or calculate expressions according to a specified period (in milliseconds), that is, to implement scheduled tasks. This can be easily achieved using Tick and Sleep in the time package. Scheduled tasks.
Example:
Use Tick to print "Hello TigerwolfC" every 100 milliseconds
for range time.Tick(time.Millisecond*100){ fmt.Println("Hello TigerwolfC") }
Print "Hello TigerwolfC" every 100 milliseconds, you can also use time. Sleep()
for{ time.Sleep(time.Millisecond* 100) fmt.Println("Hello TigerwolfC") }
func Sleep
func Sleep(d Duration)
Sleep blocks the current go coroutine for at least d time period. When d
func Tick
func Tick(d Duration) <-chan Time
Tick is a package of NewTicker and only provides access to the Ticker channel. This function is convenient if you don't need to close the Ticker.
For more golang knowledge, please pay attention to the golang tutorial column on the PHP Chinese website.
The above is the detailed content of How to schedule tasks in golang. For more information, please follow other related articles on the PHP Chinese website!