I'm not sure if this is a bug in Go or just something I don't understand. I have the following:
package main
import (
"database/sql"
"log"
"reflect"
_ "github.com/lib/pq"
)
func main() {
Db, err := sql.Open("postgres", "user=yoitsme password=openupitsme host=x.x.x.x dbname=mydb")
if err != nil {
log.Println(err)
}
rows, err := Db.Query("SELECT 1.3250::numeric, 8.548::decimal, 908.234::float, 1234::integer")
defer rows.Close()
for rows.Next() {
var col1, col2, col3, col4 interface{}
if err := rows.Scan(&col1, &col2, &col3, &col4); err != nil {
log.Println(err)
}
log.Println(col1, reflect.TypeOf(col1))
log.Println(col2, reflect.TypeOf(col2))
log.Println(col3, reflect.TypeOf(col3))
log.Println(col4, reflect.TypeOf(col4))
}
if err = rows.Err(); err != nil {
log.Println(err)
}
}
This prints:
2015/08/11 09:35:47 [49 46 51 50 53 48] []uint8
2015/08/11 09:35:47 [56 46 53 52 56] []uint8
2015/08/11 09:35:47 908.234 float64
2015/08/11 09:35:47 1234 int64
So, I get []uint8 (e.g. a string) for the first two columns that are actually numbers. The last 2 columns are as expected. According to Postgresql Numeric & Decimal types are part of the SQL standard. So, why doesn't Go follow the SQL standard in their database/sql package? Is it because Go doesn't have a builtin "Decimal" type? It seems wrong that the database/sql package turned a number into a string because of a shortcoming in the language....