I am using sqlx in Go, which is very helpful, but it does not seem to throw errors when I use struct scan and the types of the struct don't match the sql types. For example here I set up a database to have a name (string) and age(int):
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(255) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
+-------+--------------+------+-----+---------+-------+
+------+-----+
| name | age |
+------+-----+
| bob | 10 |
+------+-----+
I then use sqlx to read out the values into a struct, but the struct has the wrong types.
package main
import (
"log"
"github.com/jmoiron/sqlx"
_ "github.com/go-sql-driver/mysql"
)
// in database name is a string and age is an int
type Person struct{
Name int
Age string
}
func main() {
sqlSession, err := sqlx.Open("mysql", "root:@(localhost:3306)/dashboard?parseTime=true")
if err != nil {
log.Panic(err)
}
err = sqlSession.Ping()
if err != nil {
log.Panic(err)
}
query := "SELECT * FROM test"
rows, errSql := sqlSession.Queryx(query)
if errSql != nil {
log.Panic(errSql)
}
for rows.Next() {
var p Person
errScan := rows.StructScan(&p)
if errScan != nil {
log.Panic(errScan)
}
log.Println("Person:", p)
}
}
So instead of giving me an error, it has zeroed out values. Person: {0 }
Has anyone else run into this problem? Does any one else think this is a bug? I think it should give me an error when I try to scan into an invalid type.