I am looking to switch from lib/pg over to pgx but I cannot get a simple working select to work in pgx. Wondering if someone can point out what is wrong with this code. Why it is not working?
No problem with lib/pg but with pgx their must be something that is missing. I modified the test code from pgx example code to test out my select.
I have posted the code that I modified along with the error when code runs. I cannot see how I could have invalid memory address because the select returns. Maybe someone can point out what is going on with this code.
package main
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx"
)
var conn *pgx.Conn
var err error
func main() {
conn, err = pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err)
os.Exit(1)
}
listTasks()
defer conn.Close(context.Background())
}
func listTasks() error {
rows, _ := conn.Query(context.Background(), "select * from signal")
for rows.Next() {
var s string
var id int32
var v float64
var description string
var description2 string
err := rows.Scan(&s, &id, &v, &description, &description2)
if err != nil {
return err
}
fmt.Printf("%d. %s\n", id, description)
}
return rows.Err()
}
This is the error I get
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x4 pc=0x29eb30]
goroutine 1 [running]:
github.com/jackc/pgx.(*Conn).Query(0x0, 0x38deb8, 0x1414090, 0x3238d7, 0x14, 0x0, 0x0, 0x0, 0x14126c0, 0x192e8, ...)
/home/forex/go/src/github.com/jackc/pgx/conn.go:585 +0x18
main.listTasks(0x38deb8, 0x1414090)
/usr/local/forex/test-pgx.go:27 +0x58
main.main()
/usr/local/forex/test-pgx.go:22 +0xbc