duanlumei5941
2016-04-04 15:50
浏览 156
已采纳

Go-将NULL解析为time.Time in Struct

I am casting to a struct which has a time.Time type in.

t2 := time.Now()
format := "2006-01-02 15:04:05"

theTime, _ := time.Parse(format, t2.Format(format))

However, sometimes I don't want to set the time.Time field, how do you define this with the go/mysql db driver?

app_history := &models.AppsHistoryInsert{
    AppId:          response.SetAppData[0].Id,
    LiveDate:       &theTime,
}

Basically, I want

if(x == true) { 
    include time 
}
else {
    don't include time. 
}

I tried doing an if around the struct declaration itself and leaving the LiveDate field out but I got the error of controllers/apps.go:1068: undefined: app_history

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsa1234569 2016-04-04 16:02
    已采纳

    you need to define the app_history variable outside the if statement and then assign to it in each of the branches

    like so

    var app_history &models.AppsHistoryInsert{}
    
    if x {
      app_history = &models.AppsHistoryInsert{
        AppId:          response.SetAppData[0].Id,
        LiveDate:       &theTime,
      }
    }else {
      app_history = &models.AppsHistoryInsert{
        AppId:          response.SetAppData[0].Id,
      }
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题