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},就这样做