I have a struct like:
type Notifications struct {
Id int
Start *time.Time
}
notifications := db.GetNotifications()
So now I need to send out these notifications whenever the time matches the current time.
1 2018-11-07 09:05:00
2 2018-11-07 09:05:00
3 2018-11-07 09:15:00
..
The simplest way for me to do this is with a ticker:
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
<-ticker.C
alerts := []Notification
for _, n := range notifications {
if n.Start == // same year, month, day, hour and minute {
alerts = append(alerts, n)
}
}
sendNotifications(alerts)
// TODO mutate the notifications to avoid duplicatation sending
}
Is there a more efficient way to do be doing this?
What is the best way to match on the time, do I have to compare time.Now()'s attributes like year, month, day, hour and minute individually in my if statement? i.e. A notification is triggered if the year,month,day,hour and minute have been reached (seconds and beyond are ignored)