我正在使用 gin 框架并尝试使用 grom 进行 crud 操作。我正在尝试从 MYSQL 数据库中获取数据。我有 db.go 来获取数据库实例,每个表和模型的一些控制器我有一个这样的模型 type Campaigns struct { ID int `json:"id" form:"id" gorm:"column:CampaignID"` UserID int `json:"userId" form:"userId" gorm:"column:UserID"` Name string `json:"name" form:"name" gorm:"column:Name"` StartDate time.Time `json:"start" form:"start" gorm:"column:StartDate"` EndDate time.Time `json:"end" form:"end" gorm:"column:EndDate"` Customer string `json:"customer" form:"customer" gorm:"column:Customer"` CustomerID int `json:"customerId" form:"customerId" gorm:"column:CustomerID"` ImpressionsCounter int `json:"ImpressionsCounter" form:"ImpressionsCounter" gorm:"column:ImpressionsCounter"` MaxImpressions int `json:"maxImpressions" form:"maxImpressions" gorm:"column:MaxImpressions"` CurrentSpend float64 `json:"currentSpend" gorm:"column:CurrentSpend"` MaxSpend float64 `json:"maxSpend" form:"maxSpend" gorm:"column:MaxSpend"` Active bool `json:"active" form:"active" gorm:"column:Active"` Created time.Time `json:"created" gorm:"column:DateCreated"` Updated time.Time `json:"updated" gorm:"column:DateCreated"`}这是我正在使用的一个控制器 package controllersimport ( "time" "github.com/op/go-logging" "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" _ "github.com/go-sql-driver/mysql" "../models")var log = logging.MustGetLogger("AsAPI")type AsController struct { DB gorm.DB}func (ac *AsController) SetDB(d gorm.DB) { ac.DB = d ac.DB.LogMode(true)}
2 回答
潇湘沐
TA贡献1816条经验 获得超6个赞
您可以在驱动程序文档https://github.com/go-sql-driver/mysql#timetime-support 中找到答案 :
MySQL DATE 和 DATETIME 值的默认内部输出类型是 []byte,它允许您将值扫描到程序中的 []byte、string 或 sql.RawBytes 变量中。
然而,许多人希望将 MySQL 的 DATE 和 DATETIME 值扫描到 time.Time 变量中,这与 MySQL 中的 Go to DATE 和 DATETIME 中的逻辑相反。您可以通过使用 DSN 参数 parseTime=true 将内部输出类型从 []byte 更改为 time.Time 来实现。您可以使用 loc DSN 参数设置默认 time.Time 位置。
或者,您可以使用 NullTime 类型作为扫描目标,它适用于 time.Time 和 string / []byte。
- 2 回答
- 0 关注
- 250 浏览
添加回答
举报
0/150
提交
取消
