datetimego

How to do "greater than or equal" operation on time objects in Go?


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?


Solution

  • The proper way is:

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

    Or

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