How to handle null data in SQL for struct in golang
type account struct { id int name string age sql.NullInt64 amt sql.NullFloat64} func testReadNullvalue(db *sqlx.DB) { rows, _ := db.Query("select id, name, age , amount from account") for rows.Next() { var foo account rows.Scan(&foo.id, &foo.name, &foo.age, &foo.amt) if foo.amt.Valid==true { fmt.Println("value: %v",foo.amt.Float64 ) }else { fmt.Println("data is null, print with default value ",foo.amt.Float64 ) } fmt.Println("%+v", foo) } }
Comments
Post a Comment