Golang – 结构体:time.Time

huangapple go评论82阅读模式
英文:

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.Timeerror值。你只给一个变量赋值了。

你应该这样做:

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
}

huangapple
  • 本文由 发表于 2016年4月4日 21:53:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/36404635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定