How to run a function in golang at a certain time?

I need to run the function myfunc , for example, at 00:00. How to do it?

  • Read about the cron scheduler. It will be much more correct, and definitely more transparent than what is offered below. - Alex Belyaev
  • @AlexBelyaev, I offer nothing, I answer the question. Moreover, it all depends on the task; if the function should be launched in the context of a running application and work with its data, it is unlikely that cron will be more convenient. In general, without telepathic capabilities, it is hardly possible to say that it is β€œmore transparent” for the task ahead of the top-starter. - Vadim Shender pm
  • I agree with telepathy: that is, you answered, as you yourself understood, without asking the TS what he really needs? :) Okay, your position is clear to me, we will no longer flood. - Alex Belyaev
  • @AlexBelyaev, yes no, I read the task, and it is clearly and unambiguously written, "how to start a function in golang at a certain time," that was my answer. After, yes, when it was clarified that you need to run once a day, then doubts crept in. PS And you peace :) - Vadim Shender

1 answer 1

Well, for example like this:

 package main import ( "time" "fmt" ) // ОписаниС Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π° Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ. const timeLayout = "Jan 2, 2006 15:04:05 MST" // Π’Ρ‹Π·ΠΎΠ² ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Π½ΠΎΠΉ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Π² ΡƒΠΊΠ°Π·Π°Π½Π½ΠΎΠ΅ врСмя. func callAt(callTime string, f func()) error { // Π Π°Π·Π±ΠΈΡ€Π°Π΅ΠΌ врСмя запуска. ctime, err := time.Parse(timeLayout, callTime) if err != nil { return err } // ВычисляСм Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΠΊ Π΄ΠΎ запуска. duration := ctime.Sub(time.Now()) go func() { time.Sleep(duration) f() }() return nil } // Π’Π°ΡˆΠ° функция. func myfunc() { fmt.Printf("+ %v\n", time.Now()) } // ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования. func main() { err := callAt("Jul 17, 2015 00:00:00 MSK", myfunc) if err != nil { fmt.Printf("error: %v\n", err) } // Эмуляция дальнСйшСй Ρ€Π°Π±ΠΎΡ‚Ρ‹ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹. time.Sleep(time.Hour * 24) } 

Actually, there are many ways, for example, you can use time.After , after a specified period of time, a message will be sent to the channel. The main question here is in the calculation of this very interval; One of the possible options I brought.

Update : here's an option for everyday launch:

 package main import ( "time" "fmt" ) // Π’Ρ‹Π·ΠΎΠ² ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Π½ΠΎΠΉ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Ρ€Π°Π· Π² сутки Π² ΡƒΠΊΠ°Π·Π°Π½Π½ΠΎΠ΅ врСмя. func callAt(hour, min, sec int, f func()) error { loc, err := time.LoadLocation("Local") if err != nil { return err } // ВычисляСм врСмя ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ запуска. now := time.Now().Local() firstCallTime := time.Date( now.Year(), now.Month(), now.Day(), hour, min, sec, 0, loc) if firstCallTime.Before(now) { // Если ΠΏΠΎΠ»ΡƒΡ‡ΠΈΠ»ΠΎΡΡŒ врСмя Ρ€Π°Π½ΡŒΡˆΠ΅ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ, прибавляСм сутки. firstCallTime = firstCallTime.Add(time.Hour * 24) } // ВычисляСм Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΠΊ Π΄ΠΎ запуска. duration := firstCallTime.Sub(time.Now().Local()) go func() { time.Sleep(duration) for { f() // Π‘Π»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ запуск Ρ‡Π΅Ρ€Π΅Π· сутки. time.Sleep(time.Hour * 24) } }() return nil } // Π’Π°ΡˆΠ° функция. func myfunc() { fmt.Printf("+ %v\n", time.Now()) } // ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования. func main() { err := callAt(0, 0, 0, myfunc) if err != nil { fmt.Printf("error: %v\n", err) } // Эмуляция дальнСйшСй Ρ€Π°Π±ΠΎΡ‚Ρ‹ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹. time.Sleep(time.Hour * 24) } 
  • And if every day at 00 o'clock? - Just start yourself or another mechanism is? - Rakzin Roman
  • One more question. When once a day there will be a "cleanup" of the data at a certain time-rate. I think to create in the project for exams the event of the exam for students. And when this time comes, then run a certain function. It will be normal if, say, there are 100 current (unfinished) exams in the system (ie, respectively +100 gorutin) and they will wait in the wings, will it not load the application? - Rakzin Roman
  • On another forum offer cron - Rakzin Roman
  • Added option for everyday launch. - Vadim Shender
  • Let at least 1000 Gorutin hang in sleep, it will not load the application. - Vadim Shender pm