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

Min*_*gyu 129 time null go

在错误的情况下,我试图返回nil,这会引发错误:

cannot use nil as type time.Time in return argument
Run Code Online (Sandbox Code Playgroud)

有什么zero价值time.Time

gex*_*tra 257

您应该使用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.
Run Code Online (Sandbox Code Playgroud)

  • 虽然这是比较的正确答案,但OP并未询问比较,而是询问如何初始化零值。接受的答案是正确的。 (3认同)

zea*_*soi 128

调用空time.Time结构文字将返回Go的零日期.因此,对于以下打印声明:

fmt.Println(time.Time{})
Run Code Online (Sandbox Code Playgroud)

输出是:

0001-01-01 00:00:00 +0000 UTC
Run Code Online (Sandbox Code Playgroud)

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

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

  • "不传递参数"使它听起来像一个函数调用.它是一个没有指定字段的结构文字.X {}是任何X的struct X的零值. (6认同)

det*_*000 8

time.Time 的零值0001-01-01 00:00:00 +0000 UTC 参见http://play.golang.org/p/vTidOlmb9P