Skip to content

Instantly share code, notes, and snippets.

@pib
Last active March 3, 2016 00:11
Show Gist options
  • Select an option

  • Save pib/f8c9a4e153e6d226d2ee to your computer and use it in GitHub Desktop.

Select an option

Save pib/f8c9a4e153e6d226d2ee to your computer and use it in GitHub Desktop.
Demonstration of float scan bug in github.com/go-sql-driver/mysql
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", "root:@tcp(localhost:3306)/float_bug")
if err != nil {
log.Fatal(err)
}
printRows(db.Query("SELECT id, lat, lon FROM lat_lons WHERE id = 1"))
printRows(db.Query("SELECT id, lat, lon FROM lat_lons WHERE id = ?", 1))
}
func printRows(rows *sql.Rows, err error) {
if err != nil {
log.Fatal(err)
}
var (
id int
lat, lon *float64
)
for rows.Next() {
if err = rows.Scan(&id, &lat, &lon); err != nil {
log.Fatal(err)
}
fmt.Println(id, *lat, *lon)
}
rows.Close()
}
DROP TABLE `lat_lons`;
CREATE TABLE `lat_lons` (
`id` int,
`lat` float,
`lon` float
);
INSERT INTO `lat_lons` VALUES (1, 33.9534, -117.396);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment