无法将mysql中的时间戳值提取到time.Time变量中

真实思想:

我无法将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 |

真实思想:

错误已由我自己解决,表中有Scan无法处理的空值,因此我在struct中使用sql.NULLString或mysql.NULLTime类型,具体取决于字段

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章