先了解下time类型:
type Time struct {
// sec gives the number of seconds elapsed since
// January 1, year 1 00:00:00 UTC.
sec int64
// nsec specifies a non-negative nanosecond
// offset within the second named by Seconds.
// It must be in the range [0, 999999999].
nsec int32
// loc specifies the Location that should be used to
// determine the minute, hour, month, day, and year
// that correspond to this Time.
// Only the zero Time has a nil Location.
// In that case it is interpreted to mean UTC.
loc *Location
}
对于time.Time类型,我们可以通过使用函数Before,After,Equal来比较两个time.Time时间:
t1 := time.Now()
t2 := t1.Add(-1 * time.Minute)
fmt.Println(t1.Before(t2))
上面t1是当前时间,t2是当前时间的前一分钟,输出结果:false
对于两个time.Time时间是否相等,也可以直接使用==来判断:
t1 := time.Now()
t2 := t1.Add(-1 * time.Minute)
fmt.Println(t1 == t2)