我无法将mysql时间戳值输入到time.Time变量中
我正在尝试扫描一行,除mysql中的时间戳类型外,所有值均已成功扫描,我已经在使用dsn parseTime = true,这是我搜索到的问题之一,但无济于事
type Tags struct {
ID int
CreatedAt time.Time `json:"created_at"`
}
func foo5() {
http.HandleFunc("/tags/", bar5)
http.ListenAndServe(":8080", nil)
}
func bar5(w http.ResponseWriter, r *http.Request) {
db, err := sql.Open("mysql", "root:*******@/catalogue_service?parseTime=true")
if err != nil {
fmt.Println("error opening")
}
v, _ := strconv.Atoi(path.Base(r.URL.Path))
row := db.QueryRow("select * from tags where id = ?", v)
var myRow Tags
row.Scan(&myRow.ID, &myRow.CreatedAt)
fmt.Println(myRow)
}
int和字符串值正确显示,但CreatedAt打印 0001-01-01 00:00:00 +0000 UTC 而不是实际值
编辑:
+-----------------+--------------------------+------+-----+-------------------+-----------------------------------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------------------+------+-----+-------------------+-----------------------------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| created_at | timestamp | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
+-----------------+--------------------------+------+-----+-------------------+-----------------------------------------------+
| 84 | 2019-01-30 11:21:36 |
答案 0 :(得分:0)
答案 1 :(得分:0)
错误已由我自己解决, 表中存在Scan无法处理的空值,因此我根据字段在结构中使用了sql.NULLString或mysql.NULLTime类型