database/sql and query slice args

876 views
Skip to first unread message

Frank Davidson

unread,
Mar 25, 2015, 6:19:41 PM3/25/15
to golan...@googlegroups.com
Is there a reason that slices aren't accepted as arguments to a db.Query() in database/sql? Seems like that would make things quite a bit easier...

Just wondering...

Frank

Tamás Gulácsi

unread,
Mar 26, 2015, 1:45:19 AM3/26/15
to golan...@googlegroups.com
What should the driver do with them?

Tamás Gulácsi

unread,
Mar 26, 2015, 1:45:19 AM3/26/15
to golan...@googlegroups.com

Frank Davidson

unread,
Mar 26, 2015, 7:37:54 AM3/26/15
to golan...@googlegroups.com
Well, I would think it would make things much easier if the first query argument was set to arr[0], the second to arr[1], etc... A mismatch in type of number would error out, I would think...

Kelly Norton

unread,
Mar 26, 2015, 8:46:38 AM3/26/15
to Frank Davidson, golan...@googlegroups.com
What is preventing you from calling db.Query with a slice?

On Thu, Mar 26, 2015 at 7:38 AM Frank Davidson <ffdav...@gmail.com> wrote:
Well, I would think it would make things much easier if the first query argument was set to arr[0], the second to arr[1], etc... A mismatch in type of number would error out, I would think...

--
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.

Frank Davidson

unread,
Mar 27, 2015, 9:33:22 AM3/27/15
to golan...@googlegroups.com, ffdav...@gmail.com, k...@kellegous.com
It can only be a slice of interface{}, correct? Why can't it just be any slice? I understand that I can convert to []interface{} by why not just let us use []string or similar and let the database decide if it's the right type?


On Thursday, March 26, 2015 at 8:46:38 AM UTC-4, Kelly Norton wrote:
What is preventing you from calling db.Query with a slice?

On Thu, Mar 26, 2015 at 7:38 AM Frank Davidson <ffdav...@gmail.com> wrote:
Well, I would think it would make things much easier if the first query argument was set to arr[0], the second to arr[1], etc... A mismatch in type of number would error out, I would think...

--
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.

Tamás Gulácsi

unread,
Mar 27, 2015, 9:51:36 AM3/27/15
to golan...@googlegroups.com, ffdav...@gmail.com, k...@kellegous.com
2015. március 27., péntek 14:33:22 UTC+1 időpontban Frank Davidson a következőt írta:
It can only be a slice of interface{}, correct? Why can't it just be any slice? I understand that I can convert to []interface{} by why not just let us use []string or similar and let the database decide if it's the right type?

By using []interface{}, you let the database decide if it's the right type...

The "why can't I just convert []string to []interface{}", it is even in the FAQ, and on this list, too.

Frank Davidson

unread,
Mar 27, 2015, 10:02:48 AM3/27/15
to golan...@googlegroups.com, ffdav...@gmail.com, k...@kellegous.com
I understand I can do it, but just seems unnecessary every time i want to insert a row of strings into my database I have to convert them:

t := []int{1, 2, 3, 4}
s := make([]interface{}, len(t))
for i, v := range t {
    s[i] = v
}
I just seems to me it would be a lot easier just to accept any slice, or even a struct (though I think that would probably have performance issues, perhaps).

Carlos Castillo

unread,
Mar 27, 2015, 12:53:54 PM3/27/15
to golan...@googlegroups.com, ffdav...@gmail.com, k...@kellegous.com
It's generally not the go way to intentionally introduce unexpected performance issues into the language.

If you want you could create a function to do the []val -> []interface{} conversions if it's that much of a bother to type the for loop all the time. 

Here is a reflective version that works with any slice or array: http://play.golang.org/p/KpDqLqQW71 . Note: it will panic if the input is not a slice or an array, and it's slower than the for loop version.
Reply all
Reply to author
Forward
0 new messages