I need to process the SQL SELECT query, I don’t know how many columns I will receive, how can I send an arbitrary number of arguments to the database / sql package Scan function? You can certainly do this, but it looks too clumsy:

switch len(columns) { case 1: rows.Scan(&interfaces[0]) case 2: rows.Scan(&interfaces[0], &interfaces[1]) case 3: rows.Scan(&interfaces[0], &interfaces[1], &interfaces[2]) ... } 

This option also does not work:

 interfaces := make([]*string, 0, len(coltype)) for range interfaces{ var str string interfaces = append(interfaces, &str) } rows.Next() rows.Scan(interfaces...) 

Mistake

cannot use interfaces (type [] * string) as type [] interface {} in argument to rows.Scan

    1 answer 1

    Something like:

     args := []interface{}{&foo, &bar, &baz} // ... err = rows.Scan(args...) 

    A more detailed explanation. Inside args []interface{} should be pointers. If you are expecting one column, then:

     var foo string // ... // NB Тип «args» — «[]interface{}»; внутри — указатель. var args = []interface{}{&foo} // ... err = rows.Scan(args...) 

    If you have a line cut, then there should be something like:

     var foos []string // ... var args = make([]interface{}, len(foos)) for i := range foos { // NB Берём указатели каждой строки. args[i] = &foos[i] } // ... err = rows.Scan(args...) 
    • Tried, returns nil'y. It is necessary to transfer the link to function. - Jasar
    • If you passed pointers to data inside the interfaces, everything should work. Write a code in question like mine that doesn't work for you. - Ainar-G
    • Added your code to the question along with the error - Jasar
    • See edit. It should be clearer. - Ainar-G
    • Yes, it turned out, thanks. If it is not difficult - you can explain / give a link to the material / suggest topics for reading in order to understand why the option with append did not work? - Jasar 3:17 pm