Hello,
I cant find the reason of error in the following code. It runs successful if I use sqlx.Get directly. Do you have any idea?
package main
import (
//"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type Product struct {
Id int64 `db:"productid" json:"id"`
}
var db *sqlx.DB
func main() {
var p Product
q := "SELECT productid FROM l_product "
db, err := sqlx.Open("mysql", "getiriver:getiriver@tcp(localhost:3306)/getiriver?charset=utf8")
if err != nil {
fmt.Println(err)
}
defer db.Close()
err = GetRow(&p, q)
fmt.Println(p)
if err != nil {
fmt.Println(err)
}
db.Close()
}
func GetRow(target interface{}, q string) error {
fmt.Println(q)
fmt.Println(target)
err := db.Get(target, q)
if err != nil {
return err
}
return nil
}
end the error is
C:\Go\work\src\github.com\ligaci\test>go run test.go
SELECT productid FROM l_product
&{0}
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x594d1d]
goroutine 1 [running]:
github.com/jmoiron/sqlx.(*DB).QueryRowx(0x0, 0x600b90, 0x20, 0x0, 0x0, 0x0, 0x5)
C:/Go/work/src/github.com/jmoiron/sqlx/sqlx.go:355 +0x2d
github.com/jmoiron/sqlx.Get(0x6bd820, 0x0, 0x5ae660, 0xc04203a690, 0x600b90, 0x20, 0x0, 0x0, 0x0, 0x0, ...)
C:/Go/work/src/github.com/jmoiron/sqlx/sqlx.go:675 +0x74
github.com/jmoiron/sqlx.(*DB).Get(0x0, 0x5ae660, 0xc04203a690, 0x600b90, 0x20, 0x0, 0x0, 0x0, 0x429555, 0xc042056030)
C:/Go/work/src/github.com/jmoiron/sqlx/sqlx.go:320 +0x97
main.GetRow(0x5ae660, 0xc04203a690, 0x600b90, 0x20, 0xc042060480, 0x0)
C:/Go/work/src/github.com/ligaci/test/test.go:43 +0x15b
main.main()
C:/Go/work/src/github.com/ligaci/test/test.go:28 +0x14b
exit status 2