我正在转换为一个包含 time.Time 类型的结构。
t2 := time.Now()
format := "2006-01-02 15:04:05"
theTime, _ := time.Parse(format, t2.Format(format))
但是有时候不想设置time.Time字段,go/mysql db driver怎么定义呢?
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 的错误:未定义: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,
}
}
关于Go - 将 NULL 解析为 time.Time in Struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36407297/