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.

Revisions

  1. pib revised this gist Mar 3, 2016. 3 changed files with 13 additions and 4 deletions.
    8 changes: 5 additions & 3 deletions float_bug.go
    Original file line number Diff line number Diff line change
    @@ -14,8 +14,10 @@ func main() {
    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))
    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))

    }

    @@ -36,4 +38,4 @@ func printRows(rows *sql.Rows, err error) {
    fmt.Println(id, *lat, *lon)
    }
    rows.Close()
    }
    }
    2 changes: 1 addition & 1 deletion float_bug.sql
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    DROP TABLE `lat_lons`;
    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);
    7 changes: 7 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    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
  2. pib created this gist Mar 3, 2016.
    39 changes: 39 additions & 0 deletions float_bug.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    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()
    }
    7 changes: 7 additions & 0 deletions float_bug.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    DROP TABLE `lat_lons`;
    CREATE TABLE `lat_lons` (
    `id` int,
    `lat` float,
    `lon` float
    );
    INSERT INTO `lat_lons` VALUES (1, 33.9534, -117.396);