So I found this: https://betterprogramming.pub/dynamic-sql-query-with-go-8aeedaa02907
it explains how to create a dynamic query for the basic sql package. I would like to do the same thing in pgx but i cant get it to work. Here is my code:
func (db *Database) Query(query string, queryParams ...interface{}) {
rows, _ := db.conn.Query(context.Background(), query)
defer rows.Close()
fieldDescriptions := rows.FieldDescriptions()
var cols []string
for _, fieldDesc := range fieldDescriptions {
cols = append(cols, string(fieldDesc.Name))
}
row := make([][]byte, len(cols))
rowPtr := make([]interface{}, len(cols))
for i := range row {
rowPtr[i] = &row[i]
}
for rows.Next() {
err := rows.Scan(rowPtr...)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(rowPtr)
}
}
With this I get an error:
can't scan into dest[0]: cannot assign 1 into *[]uint8
The first item in the row is an id, the first has 1, so its trying to put the int into a *[]uint8, no idea whats going on there.
If i take out row stuff the
row := make([][]byte, len(cols))
for i := range row {
rowPtr[i] = &row[i]
}
and just try to read directly to a variadic array of interface{} I would have thought it would read into it, since the Scan takes an interface{}, but i just get nil for everything. Anyone able to shed any light on any of this?