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

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,
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题