0

Query:

`select "number" from "person"`

Here is the code where am iterating over a row. number is of type float4 in Postgres. I am using pgtype.Float4 for scanning.

    for rows.Next() {
        var number pgtype.Float4
        err := rows.Scan(number)
        if err != nil {
            panic(err)
        }
    }

I am getting the following error:

panic: can't scan into dest[2]: cannot assign 5000 into pgtype.Float4

What might be the problem? Any suggestion is welcome. Thank you in advance.

dclipca
  • 1,739
  • 1
  • 16
  • 51
  • 2
    The error doesn't match the code, i.e. it says `dest[2]` but you're passing only single a dest to Scan. Also please include the query SQL. – mkopriva Aug 25 '20 at 10:29
  • @mkopriva in this case I would get something like: panic: number of field descriptions must equal number of destinations, got x+ 1 and x – dclipca Aug 25 '20 at 11:46
  • 1
    So you're saying that that sql query, together with that go code produce the `dest[2]` in the panic? That seems weird, that digit is supposed to be the 0-indexed position of the column being scanned. If it's really that code producing that error then it may be a pgx bug. – mkopriva Aug 25 '20 at 11:53
  • 1
    If you can provide a fully reproducible example you should probably open an issue on pgx's github page. – mkopriva Aug 25 '20 at 11:55
  • This is my bad. The problem is not in the number of dests but rather in the data type. I simplified the code a bit to show the essence of the problem. It says dest[2] because ```number``` is the 3rd variable. – dclipca Aug 25 '20 at 11:59
  • 1
    When you're simplifying broken code for the purposes of soliciting help you need to make absolutely sure that that simplified version still produces the error you're trying to fix. If you simplify the code to the point where it no longer produces the error then it will make no sense to the people that are trying to help you. Before you post the simplified version, try it out first, does it break? If not don't post it. It's called [minimal **reproducible** example](https://stackoverflow.com/help/minimal-reproducible-example), or `mcve` as in **complete and verifiable** for a reason. – mkopriva Aug 25 '20 at 12:06

1 Answers1

2

I simply missed & before my variable name in .Scan().

dclipca
  • 1,739
  • 1
  • 16
  • 51