Skip to content

Instantly share code, notes, and snippets.

@pib
Last active March 3, 2016 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pib/f8c9a4e153e6d226d2ee to your computer and use it in GitHub Desktop.
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)
}
fmt.Println("With no placeholders:")
printRows(db.Query("SELECT id, lat, lon FROM lat_lons WHERE id IN(1, 2)"))
fmt.Println("\nWith placeholder:")
printRows(db.Query("SELECT id, lat, lon FROM lat_lons WHERE id IN(?, ?)", 1, 2))
}
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()
}
CREATE TABLE `lat_lons` (
`id` int,
`lat` float,
`lon` float
);
INSERT INTO `lat_lons` VALUES (1, 33.9534, -117.396);
INSERT INTO `lat_lons` VALUES (2, 35.7942, -115.401);
With no placeholders:
1 33.9534 -117.396
2 35.7942 -115.401
With placeholder:
1 33.953399658203125 -117.39600372314453
2 35.7942008972168 -115.4010009765625
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment