3

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
user10078199
  • 141
  • 2
  • 8

2 Answers2

3

You are redefining conn below:

conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))

So you're not assigning the global conn.

Change that to:

var err error
conn, err=px.Connect(...)

According to language spec:

Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new.

conn is not defined in the same block, so the short declaration is defining conn and err, instead of assigning to conn.

Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
  • When I put var err error as global I just get a return of nothing. Like the select is not even being executive. – user10078199 Feb 12 '20 at 00:57
  • Now I get a return on err of can't scan into dest[3]: unable to assign to *string I do not completely get. – user10078199 Feb 12 '20 at 01:04
  • Do not declare a global error variable. Declare it locally. – Burak Serdar Feb 12 '20 at 01:17
  • The error is self-explanatory: underlying column value cannot be assigned to a *string. Instead of selec *, use colum names, and see what types of columns you're dealing with. – Burak Serdar Feb 12 '20 at 01:18
  • That dose not appear to be the issue their is something I am doing wrong with Scan function call. rows.Next(): can't scan into dest[1]: cannot assign 1 into *string the return is not being type cast from the cursor being returned by postgresql – user10078199 Feb 12 '20 at 01:22
  • Maybe you can ask that as a separate question, someone else might be able to help. – Burak Serdar Feb 12 '20 at 04:03
  • I cannot find any simple code that works for pgx. For now I am giving up and going back to lib/pg that works with zero problems. Why pgx is so hard to use I do not understand. – user10078199 Feb 12 '20 at 04:52
1

invalid memory address or nil pointer dereference anytime i see an error like this usually it means am trying to assign a value to a nil pointer. But in your case i couldn't found any. Well i found Nothing wrong with this code i did the same thing and it works. you can take a look HERE. sometimes os.Getenv don't load the .env file so it result to file not found, so try to copy the database source to the pgx as show in the example code.