golang读mysql中的null值的问题

mysql中有4个字段
goods_id goods_name goods_desc bref
1 a null test1

rows.Scan(&goods_id,&goods_name,&goods_desc,&bref)

读到这行的时候,如果goods_desc为空。那么后面的bref取到的值也是空的。

共 1 个回复


fanyang

package main

import (
    "fmt"

    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mysql"
)

func main() {
    db, err := gorm.Open("mysql", "root:mima@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local")
    if err != nil {
        panic(err)
    }
    defer db.Close()

    rows, err := db.Raw(`SELECT * from goods`).Rows()
    if err != nil {
        panic(err)
    }
    for rows.Next() {
        var (
            goods_id                     int
            goods_name, goods_desc, bref string
        )

        err = rows.Scan(&goods_id, &goods_name, &goods_desc, &bref)
        if err != nil {
            panic(err)
        }
        fmt.Println(goods_id, goods_name, goods_desc, bref)
    }
}

# 0