提问人:WoJ 提问时间:4/11/2022 更新时间:4/11/2022 访问量:459
如何将解析的日期与空日期进行比较?[复制]
How can I compare a parsed date to an empty date? [duplicate]
问:
我收到一条来自用 Go 编写的 API 的 JSON 消息。其中一个字段是日期,它可能是创建 JSON 时转换为的字段。time.Time{}
"0001-01-01T00:00:00Z"
在接收端,我将把JSON消息解组到一个与消息结构匹配的变量中,其中一个字段是我上面提到的日期(类型)。time.Time
我想对照一个空的检查它。
我期望下面的代码没问题,但事实并非如此:
package main
import (
"fmt"
"time"
)
func main() {
receivedDate := "0001-01-01T00:00:00Z" // this mocks the content of the received field
receivedDateParsed, _ := time.Parse(time.RFC3339, receivedDate) // this is equivlent to the unmarshalling into the message stricture
if receivedDateParsed == time.Time{} { // my comparison
fmt.Print("date is empty")
}
}
错误是,但我不确定这在我使用的上下文中意味着什么。Type 'time.Time' is not an expression
time.Time{}
答: 暂无答案
评论
time.Time{}
()
==
time.Time
IsZero