gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:05:52 26 4
gpt4 key购买 nike

我正在转换为一个包含 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com