G
G
Georgy Pugachev2019-11-25 00:24:15
PostgreSQL
Georgy Pugachev, 2019-11-25 00:24:15

How to get parameters by name?

Hello, using the postgresql package, I usually get query result parameters like this:
err := rows.Scan(&list.ID, &list.Name)
Now I'm interested in getting parameters by name, for example:

list.ID := rows.byName["id"]
list.Name := rows.byName["name"]

Is it possible?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Mamonov, 2019-11-25
@gvpugachev

It is possible to do it, for example, using this very good package jmoiron.github.io/sqlx
There are many useful examples here jmoiron.github.io/sqlx
Specifically for your case, MapScan is suitable, here is an example:

rows, err := db.Queryx("SELECT * FROM place")
for rows.Next() {
    results := make(map[string]interface{})
    err = rows.MapScan(results)
}

P
Papa, 2019-11-25
Stifflera @PapaStifflera

See the package documentation.
What you gave as an example is impossible in principle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question