2

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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

When I put var err error as global I just get a return of nothing. Like the select is not even being executive.
Now I get a return on err of can't scan into dest[3]: unable to assign to *string I do not completely get.
Do not declare a global error variable. Declare it locally.
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.
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
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.