How to schedule tasks in golang

Release: 2020-01-14 09:34:24
Original
3882 people have browsed it

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") 
}
Copy after login

Print "Hello TigerwolfC" every 100 milliseconds, you can also use time. Sleep()

for{
	time.Sleep(time.Millisecond* 100)
	fmt.Println("Hello TigerwolfC")
}
Copy after login

func Sleep

func Sleep(d Duration)
Copy after login

Sleep blocks the current go coroutine for at least d time period. When d <= 0, Sleep will return immediately.

func Tick

func Tick(d Duration) <-chan Time
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template