FAQ
Trying to do a sql rows.Scan on an arbitrary sql select.

rows, err := stmt.Query(args.Params...)
columns, err := rows.Columns()

for rows.Next() {
   row := make([]string, len(columns))
   err = rows.Scan(&row[0], &row[1])
}

Now i want to call the rows.Scan using something like:

err = rows.Scan(row...)

But this gives an error: cannot use row (type []string) as type []interface
{} in argument to rows.Scan

What is the correct syntax to use in this case?

Best regards,
Sander

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Andy Balholm at Sep 10, 2014 at 6:09 pm

    On Sep 10, 2014, at 8:32 AM, sanderhahn@gmail.com wrote:

    Trying to do a sql rows.Scan on an arbitrary sql select.

    rows, err := stmt.Query(args.Params...)
    columns, err := rows.Columns()

    for rows.Next() {
    row := make([]string, len(columns))
    err = rows.Scan(&row[0], &row[1])
    }

    Now i want to call the rows.Scan using something like:

    err = rows.Scan(row...)

    But this gives an error: cannot use row (type []string) as type []interface {} in argument to rows.Scan

    What is the correct syntax to use in this case?
    You need to make a slice of pointers to the strings in row, with type []interface{} to match Scan's signature.

    p := make([]interface{}, len(row))
    for i := range row {
      p[i] = &row[i]
    }
    err = rows.Scan(p...)

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 10, '14 at 3:32p
activeSep 10, '14 at 6:09p
posts2
users2
websitegolang.org

2 users in discussion

Sanderhahn: 1 post Andy Balholm: 1 post

People

Translate

site design / logo © 2021 Grokbase