1

I am slightly confused by the output i'm receiving from my Postgres when querying it with the use of go. Since I am very new to this I have a hard time even forming the right question for this problem I have, so I'll just leave a code block here, with the output I'm receiving and what I expected to happen. I hope this makes it more understandable.

The connection to the postgres db seems to work fine

        rows, err := db.Query("SELECT title FROM blogs;")                                                                                                                                                      
        fmt.Println("output", rows) 

However, this is the output I am receiving.

output &{0xc4200ea180 0x4c0e20 0xc42009a3c0 0x4b4f90 <nil> {{0 0} 0 0 0 0} false <nil> []}

As I said, I am new to postgres and go, and I have no Idea what I am dealing with here.

I was expecting my entire table to return in a somewhat readable format.

1

1 Answer 1

4

I was expecting my entire table to return in a somewhat readable format.

It does not come back in a "readable" format, why would it?

Query returns a struct that you can use to iterate through the rows that matched the query.

Adapting the example in the docs to your case, and assuming your title field is a VARCHAR, something like this should work for you:

rows, err := db.Query("SELECT title FROM blogs;")
if err != nil {
    log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
    var title string
    if err := rows.Scan(&title); err != nil {
            log.Fatal(err)
    }
    fmt.Println(title)
}
if err := rows.Err(); err != nil {
    log.Fatal(err)
}
Sign up to request clarification or add additional context in comments.

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.