Answer the question
In order to leave comments, you need to log in
How to get response as array from database?
Good afternoon!
I'm trying to figure out how to work with a database in golang.
As a DBMS I have postgres I
wrote the following code:
func main() {
db, err := sql.Open("postgres", config.PostgresqlConfig)
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query("SELECT * FROM tablename")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var a, b string
err := rows.Scan(&a, &b)
if err != nil {
fmt.Println(err)
continue
}
fmt.Println(a, b)
}
}
err := rows.Scan(&a, &b)
Answer the question
In order to leave comments, you need to log in
It is most convenient to work with the database in golang using ORM. For example gorm .
Inside the loop, just insert your variables into the slice, and after the loop, process the resulting slice
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question