Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go-将NULL解析为结构中的time.time_Go - Fatal编程技术网

Go-将NULL解析为结构中的time.time

Go-将NULL解析为结构中的time.time,go,Go,我正在强制转换到一个有时间的结构。输入时间 t2 := time.Now() format := "2006-01-02 15:04:05" theTime, _ := time.Parse(format, t2.Format(format)) 但是,有时我不想设置time.time字段,您如何使用go/mysql数据库驱动程序定义它 app_history := &models.AppsHistoryInsert{ AppId: response.SetA

我正在强制转换到一个有时间的结构。输入时间

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

theTime, _ := time.Parse(format, t2.Format(format))
但是,有时我不想设置time.time字段,您如何使用go/mysql数据库驱动程序定义它

app_history := &models.AppsHistoryInsert{
    AppId:          response.SetAppData[0].Id,
    LiveDate:       &theTime,
}
基本上,我想要

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

我试图围绕结构声明本身执行
if
,并保留
LiveDate
字段,但我得到了
controllers/apps的错误。go:1068:undefined:app_history

您需要在if语句之外定义app_history变量,然后在每个分支中分配给它

像这样

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,
  }
}

或者,以防在现实中有更多的初始化。现在就按原样做,但排除“LiveDate”,然后在创建初始结构后,如果x{app_history.LiveDate=&theTime},就这样做