小编典典

Go 中 time.Time 的“零”值是多少?

go

在错误情况下,我尝试 return nil,这引发了错误:

cannot use nil as type time.Time in return argument

zero价值是time.Time什么?


阅读 534

收藏
2021-11-03

共2个答案

小编典典

调用空time.Time结构体字面量将返回 Go 的零日期。因此,对于以下打印语句:

fmt.Println(time.Time{})

输出是:

0001-01-01 00:00:00 +0000 UTC

为了完整起见,官方文档明确指出:

Time 类型的零值是 1 月 1 日,第 1 年,00:00:00.000000000 UTC。

2021-11-03
小编典典

您应该改用 Time.IsZero() 函数:

func (Time) IsZero

func (t Time) IsZero() bool
IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.
2021-11-03