英文:
Golang - struct : time.Time
问题
我正在尝试将一个值转换为类型为time.Time的结构体。
该值为:
t := time.Now()
format := "2006-01-02 15:04:05"
然后我试图将其放入结构体中:
response.SetAppData[0].LiveDate = time.Parse(format, t.String())
然而,我遇到了以下错误:
controllers/apps.go:1085: multiple-value time.Parse() in single-value context
我不确定我做错了什么。
谢谢
英文:
I am trying to cast a value to a struct which has a type of time.Time.
The value is:
t := time.Now()
format := "2006-01-02 15:04:05"
Then I am trying to put this into the struct:
response.SetAppData[0].LiveDate = time.Parse(format, t.String())
However I get the error of:
controllers/apps.go:1085: multiple-value time.Parse() in single-value context
I am not sure what I am doing wrong.
Thanks
答案1
得分: 2
这意味着time.Parse
返回两个结果,即time.Time
和error
值。你只给一个变量赋值了。
你应该这样做:
response.SetAppData[0].LiveDate, err = time.Parse(format, t.String())
if err != nil {
// 在这里处理错误
}
英文:
It means that time.Parse
returns two results time.Time
and error
values. You are assigning only to one variable.
You should do that:
response.SetAppData[0].LiveDate, err = time.Parse(format, t.String())
if err != nil {
// error handling here
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论