我正在使用 gin 框架并嘗試使用 grom 進行 crud 操作。我正在嘗試從 MYSQL 數(shù)據(jù)庫中獲取數(shù)據(jù)。我有 db.go 來獲取數(shù)據(jù)庫實例,每個表和模型的一些控制器我有一個這樣的模型 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條經(jīng)驗 獲得超6個贊
您可以在驅(qū)動程序文檔https://github.com/go-sql-driver/mysql#timetime-support 中找到答案 :
MySQL DATE 和 DATETIME 值的默認內(nèi)部輸出類型是 []byte,它允許您將值掃描到程序中的 []byte、string 或 sql.RawBytes 變量中。
然而,許多人希望將 MySQL 的 DATE 和 DATETIME 值掃描到 time.Time 變量中,這與 MySQL 中的 Go to DATE 和 DATETIME 中的邏輯相反。您可以通過使用 DSN 參數(shù) parseTime=true 將內(nèi)部輸出類型從 []byte 更改為 time.Time 來實現(xiàn)。您可以使用 loc DSN 參數(shù)設(shè)置默認 time.Time 位置。
或者,您可以使用 NullTime 類型作為掃描目標,它適用于 time.Time 和 string / []byte。
- 2 回答
- 0 關(guān)注
- 194 瀏覽
添加回答
舉報
0/150
提交
取消