I'm using PostgreSQL with , and I'm trying to run a SELECT * FROM query in Pgx.
I can't seem to get the iteration down, as it only returns the last key in the table. I'm also trying to serve this as JSON on running the echo HTTP server library.
main.go function ( importing connection.Conn from my database connection file)
func getLanguages(c echo.Context) (err error) {
Conn := connection.Conn
type Row struct {
Id int
Language string
Name string
}
rowArray := Row{}
rows, err := Conn.Query(context.Background(), "SELECT * FROM languages")
defer rows.Close()
// rowsCount := 0
for rows.Next() {
err := rows.Scan(&rowArray.Id, &rowArray.Language, &rowArray.Name)
if err != nil {
log.Fatal(err)
}
}
fmt.Println(rowArray)
return c.JSON(http.StatusOK, rowArray)
}
Expected Output
id | language | name
----+------------+---------------------
1 | none | Plaintext
2 | go | Golang
3 | python | Python
4 | js | JavaScript
5 | jsx | React JSX
6 | ts | TypeScript
7 | tsx | React TSX
8 | tsconfig | TSConfig
9 | rb | Ruby
10 | sql | SQL
11 | sol | Solidity (Ethereum)
12 | html | HTML
13 | css | CSS
14 | csharp | C#
15 | haskell | Haskell
16 | rust | Rust
17 | scala | Scala
18 | svg | SVG
19 | graphql | GraphQL
20 | php | PHP
21 | powershell | PowerShell
22 | yaml | YAML
23 | json | JSON
Actual Output
{"Id":23,"Language":"json","Name":"JSON"}
Any help?