猿问

在 Golang 中将字符串转换为时间结构

我的方法以这种格式注册当前时间

now = time.Now().format(time.RFC3339)

我想把它分配给

message.LastReadAt = &now // field LastReadAt *time.Time`example:"2020-05-01T15:00:00Z"` of Message type

但是当我尝试“message.LastReadAt = &now”时收到此消息

Cannot use '&now' (type *string) as type *time.Time

如何将变量“now”转换回“*time.Time”类型?


慕容森
浏览 179回答 1
1回答

白衣染霜花

我同意Sheshnath。看起来您需要使用time.Parse https://golang.org/pkg/time/#Parse。由于您已经指定了格式,time.RFC3339在您使用 时time.Now(),您应该能够做到t, _ := time.Parse(time.RFC3339, now) message.LastReadAt = t
随时随地看视频慕课网APP

相关分类

Go
我要回答