How to pass parameters to a function all rows in an array

2

I need to pass all the rows of an array as parameters of a function, but I can not make it dynamic ...

What I want to make dynamic

args = append(args, u.yy)
args = append(args, u.xx)
args = append(args, u.dd)
args = append(args, u.kk)

smt.Exec(args[0], args[0], args[3], args[4])

What I'm doing

args = append(args, u.yy)
args = append(args, u.xx)
args = append(args, u.dd)
args = append(args, u.kk)

smt.Exec(args)
//ou
smt.Exec(args[:])

But it's not working ... Can you help me?

    
asked by anonymous 23.08.2018 / 14:23

1 answer

2

Using variadic functions:

    package main

    import "fmt"

    // Here's a function that will take an arbitrary number
    // of 'int's as arguments.
    func sum(nums ...int) {
        fmt.Print(nums, " ")
        total := 0
        for _, num := range nums {
            total += num
        }
        fmt.Println(total)
    }

    func main() {

        // Variadic functions can be called in the usual way
        // with individual arguments.
        sum(1, 2)
        sum(1, 2, 3)

        // If you already have multiple args in a slice,
        // apply them to a variadic function using
        // 'func(slice...)' like this.
        nums := []int{1, 2, 3, 4}
        sum(nums...)
    }

More information link and link .

    
23.08.2018 / 17:08