可以使用 time 包中的函数进行时间比较。下面是一个示例:
package main
import (
"fmt"
"time"
)
func main() {
t1 := time.Date(2020, time.Month(11), 1, 12, 0, 0, 0, time.UTC)
t2 := time.Date(2020, time.Month(11), 1, 12, 30, 0, 0, time.UTC)
if t1.After(t2) {
fmt.Println("t1 is after t2")
} else if t1.Before(t2) {
fmt.Println("t1 is before t2")
} else {
fmt.Println("t1 is equal to t2")
}
}
在上面的示例中,我们创建了两个时间 t1 和 t2。通过使用 After 和 Before 函数,我们可以比较这两个时间。如果 t1 比 t2 晚,则输出“t1 is after t2”;如果 t1 比 t2 早,则输出“t1 is before t2”;否则输出“t1 is equal to t2”。