簡體   English   中英

在 Golang 中將字符串轉換為時間結構

[英]Convert string to time struct in 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”類型?

我同意謝什納特的觀點。 看起來您需要使用time.Parse https://golang.org/pkg/time/#Parse

由於您已經在使用time.Now()指定了格式time.RFC3339 ,您應該能夠做到

t, _ := time.Parse(time.RFC3339, now)
message.LastReadAt = t

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM