在 Go 中解析动态时间格式

Parsing Dynamic Time Format in Go

我正在使用外部 API 如果最后一个值为零,它有时会缩短返回的时间戳。

layout := "2006-01-02T15:04:05.000"
opened, err := time.Parse(layout, externallyFormattedTimestamp)
if err != nil {
    fmt.Println("something went wrong parsing the timestamp")
    fmt.Println(err.Error())
}

如果返回的 externallyFormattedTimestamp 是 2018-11-05T06:19:59.827,这将毫无问题地解析。

但如果返回的时间以零结尾,它将被缩短为 2018-11-05T06:19:59.8 而不是 2018-11-05T06:19:59.800,然后中断。我是否需要在解析之前手动将零附加到字符串,或者我错过了 Go 中内置的东西可以为我处理这个问题?

改为使用此布局:2006-01-02T15:04:05.999