GO_INTERVIEW_LIB Telegram 546
Вы создаете приложение, которому необходимо выполнять код через определённый промежуток времени или с регулярными интервалами. Какие пакеты будете использовать?

Go предоставляет для этого time.Timer и time.Ticker.

• Timers: срабатывают один раз по истечении заданного времени.
• Tickers: запускаются повторно с фиксированными интервалами.

Вот как можно использовать функцию time.Timer, чтобы подождать 2 секунды перед выполнением кода:

package main

import (
"fmt"
"time"
)

func main() {
timer := time.NewTimer(2 * time.Second)
<-timer.C
fmt.Println("Timer expired!")
}


а вот и ticker, который запускается каждую секунду:

package main

import (
"fmt"
"time"
)

func main() {
ticker := time.NewTicker(1 * time.Second)
for t := range ticker.C {
fmt.Println("Tick at", t)
}
}
👍22



tgoop.com/go_interview_lib/546
Create:
Last Update:

Вы создаете приложение, которому необходимо выполнять код через определённый промежуток времени или с регулярными интервалами. Какие пакеты будете использовать?

Go предоставляет для этого time.Timer и time.Ticker.

• Timers: срабатывают один раз по истечении заданного времени.
• Tickers: запускаются повторно с фиксированными интервалами.

Вот как можно использовать функцию time.Timer, чтобы подождать 2 секунды перед выполнением кода:

package main

import (
"fmt"
"time"
)

func main() {
timer := time.NewTimer(2 * time.Second)
<-timer.C
fmt.Println("Timer expired!")
}


а вот и ticker, который запускается каждую секунду:

package main

import (
"fmt"
"time"
)

func main() {
ticker := time.NewTicker(1 * time.Second)
for t := range ticker.C {
fmt.Println("Tick at", t)
}
}

BY Библиотека Go для собеса | вопросы с собеседований


Share with your friend now:
tgoop.com/go_interview_lib/546

View MORE
Open in Telegram


Telegram News

Date: |

“Hey degen, are you stressed? Just let it all out,” he wrote, along with a link to join the group. Among the requests, the Brazilian electoral Court wanted to know if they could obtain data on the origins of malicious content posted on the platform. According to the TSE, this would enable the authorities to track false content and identify the user responsible for publishing it in the first place. A Hong Kong protester with a petrol bomb. File photo: Dylan Hollingsworth/HKFP. On Tuesday, some local media outlets included Sing Tao Daily cited sources as saying the Hong Kong government was considering restricting access to Telegram. Privacy Commissioner for Personal Data Ada Chung told to the Legislative Council on Monday that government officials, police and lawmakers remain the targets of “doxxing” despite a privacy law amendment last year that criminalised the malicious disclosure of personal information. Click “Save” ;
from us


Telegram Библиотека Go для собеса | вопросы с собеседований
FROM American