$ go run sample.go
2017/11/02 02:37:13 error: sql: Scan error on column index 1: converting driver.Value type <nil> ("<nil>") to a float64: invalid syntax
2017/11/02 02:37:13 error: sql: Scan error on column index 0: converting driver.Value type <nil> ("<nil>") to a float64: invalid syntax
Last active
November 2, 2017 10:23
-
-
Save odeke-em/78a237c1b4788ad36162c47e5903217a to your computer and use it in GitHub Desktop.
Repro to support proposal to support parsing out `<nil>`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/database/sql/convert.go b/src/database/sql/convert.go | |
index b79ec3f..bb384a9 100644 | |
--- a/src/database/sql/convert.go | |
+++ b/src/database/sql/convert.go | |
@@ -402,11 +402,15 @@ func convertAssign(dest, src interface{}) error { | |
dv.SetUint(u64) | |
return nil | |
case reflect.Float32, reflect.Float64: | |
- s := asString(src) | |
- f64, err := strconv.ParseFloat(s, dv.Type().Bits()) | |
- if err != nil { | |
- err = strconvErr(err) | |
- return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err) | |
+ var f64 float64 | |
+ if src != nil { | |
+ var err error | |
+ s := asString(src) | |
+ f64, err = strconv.ParseFloat(s, dv.Type().Bits()) | |
+ if err != nil { | |
+ err = strconvErr(err) | |
+ return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err) | |
+ } | |
} | |
dv.SetFloat(f64) | |
return nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"flag" | |
"log" | |
// MySQL driver | |
_ "github.com/go-sql-driver/mysql" | |
) | |
type trade struct { | |
DBID uint64 | |
Buy float64 | |
Sell float64 | |
Exch string | |
} | |
func main() { | |
var dbURL string | |
flag.StringVar(&dbURL, "db-url", "root:@/trades", "the URL to connect to MySQL") | |
flag.Parse() | |
db, err := sql.Open("mysql", dbURL) | |
if err != nil { | |
log.Fatalf("openDB: %v", err) | |
} | |
setup := []string{ | |
"create database if not exists repro", | |
"use repro", | |
`create table if not exists repro( | |
id integer not null AUTO_INCREMENT, | |
buy float(53), | |
sell float(53), | |
exch varchar(128), | |
primary key(id) | |
);`, | |
"insert into repro (buy, exch) values(10, 'gophexch')", | |
"insert into repro (sell, exch) values(-97.6, 'goos')", | |
} | |
for i, line := range setup { | |
if _, err := db.Exec(line); err != nil { | |
log.Fatalf("line: #%d err: %v", i, err) | |
} | |
} | |
rows, err := db.Query("select buy, sell, exch from repro") | |
if err != nil { | |
log.Fatalf("row err: %v", err) | |
} | |
for rows.Next() { | |
curTrade := new(trade) | |
err := rows.Scan(&curTrade.Buy, &curTrade.Sell, &curTrade.Exch) | |
if err != nil { | |
log.Printf("error: %v\n", err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment