Go time

In addition to the functionalities shared by other programming languages, such as acquiring year, month, day, hour, minute, second, and timestamp, the time package in Go offers two intriguing and practical features: delayed execution and periodic execution.

After

time.After is often used for delayed execution of a task: execute the task after a specified duration of xxx time.

//delayed execution
delay := 5 * time.Second
select {
case <-time.After(delay):
    fmt.Println("Timeout!")
}

Tick

time.Tick is commonly used for periodic execution of a task: execute the task every xxx duration of time.

//periodic execution
interval := 2 * time.Second
ticker := time.Tick(interval)
//method 1
for {
    select {
        case <-ticker:
            fmt.Println("Tick1:", time.Now().Unix())
    }
}

//method 2
for range ticker {
    fmt.Println("Tick2:", time.Now().Unix())
}

 

Take a break

πŸ‘‰πŸ‘‰πŸ‘‰ 【Jade Dynasty EP50】Gui Li and Xiao Bai assisted the Jin Clan in resisting the invasion