Junior — Middle
How can tasks be executed without using the built-in event or task scheduler?
sobes.tech AI
Answer from AI
If you cannot use built-in schedulers (like cron or internal timers), you can implement task execution using goroutines and channels in Go.
For example, you can create an infinite loop in a separate goroutine that waits for a specified time and then manually runs a task:
package main
import (
"fmt"
"time"
)
func task() {
fmt.Println("Executing task", time.Now())
}
func main() {
go func() {
for {
task()
time.Sleep(1 * time.Hour) // delay between executions
}
}()
select {} // block main to prevent program from exiting
}
This way, you can manage scheduling manually without built-in schedulers.