Destination not a pointer

1,088 views
Skip to first unread message

Josh Kamau

unread,
May 29, 2014, 5:01:33 PM5/29/14
to golang-nuts
Why does this tell me destination is not a pointer?

values := make([]interface{}, len(columns))
err := rows.Scan(values...)

Thanks
Josh

DV

unread,
May 29, 2014, 5:34:44 PM5/29/14
to golan...@googlegroups.com
The same reason this needs pointers:

Go is pass by value. Passing in an argument creates a *copy* of that argument. So you end up modifying the copy, not the original. You're passing *copies* of your slice contents. "Scan" is being nice and letting you know you're doing something wrong, by examining what you passed in using reflection. 

Josh Kamau

unread,
May 29, 2014, 6:40:19 PM5/29/14
to DV, golang-nuts
HI DV ;

The challenge i have is  i am dealing with []interface {} ,  i tried passing &values but it didnt work. I even trying constructing the array in a loop using append(values, &valueInterface) so that i endup with []*value where a value is interface{} but it didnt work either.

Thanks
Josh


--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Josh Kamau

unread,
May 29, 2014, 7:06:17 PM5/29/14
to DV, golang-nuts
This sort of  worked.

       var values = make([]interface{}, len(columns))
        for i, _ := range columns {
            values[i] = new(interface{})
        }
        err := rows.Scan(values...)

Dave Cheney

unread,
Jun 4, 2014, 6:59:42 AM6/4/14
to golan...@googlegroups.com
That doesn't make sense, you are asking rows.Scan to unpack each value into an interface{}, not a discrete type. Why don't you know the type of the columns you have requested ?

Josh Kamau

unread,
Jun 4, 2014, 7:11:49 AM6/4/14
to Dave Cheney, golang-nuts
I was creating a generic function.  I ended up using gorp which does what i was trying to do. 

Thanks.
Josh


--

Dave Cheney

unread,
Jun 4, 2014, 7:17:39 AM6/4/14
to Josh Kamau, golang-nuts
Right, thanks for confirming, in that case what you have done is not
the same as gorp

// gorp
s := make([]interface{}. 3)
s[0] = new(int)
s[1] = new(bool)
s[2] = new(string)

so rows.Scan(s...) will be able to interogate the types inside s

filling s with new(interface{}) won't give rows.Scan the information
it needs to unpack the data from the sql driver.
Reply all
Reply to author
Forward
0 new messages