Home > Software design >  How to do "greater than or equal" operation on time objects in go?
How to do "greater than or equal" operation on time objects in go?

Time:11-04

How do I write something like this in go?

if time.Now() >= expiry {
}

If I use time.Now().After(expiry), then it is not really the greater than or equal (>=) logic that I am looking for. If I use time.Now().After(expiry) || time.Now() == expiry, then the expression looks long. Is there a "proper" way to do this?

CodePudding user response:

The proper way is:

now:=time.Now()
if now.After(expiry) || now.Equal(expiry) {
  ...
}

Or

if !time.Now().Before(expiry) {
  ...
}
  • Related