How can I pass a slice as a variadic input?

golang pass slice to variadic
corresponding parameter is non-variadic
invalid use of ..., corresponding parameter is non-variadic
can only use ... with matching parameter
golang slice
golang pass array as varargs
variadic functions
golang variadic return

I have a function func more(... t). I'm wondering if it's possible to use a slice to populate a list of arguments ... .

I'm trying to solve the following program. Basically to mimic a normal shell which receives the command as a string. Command function requires a "list" of arguments and I don't see how I can convert a string into a such list

    import "os/exec"
    import "strings"
    func main(){
        plainCommand  := "echo hello world"
        sliceA := strings.Fields(plainCommand)
        cmd := exec.Command(sliceA)
    }

The Go Programming Language Specification

Passing arguments to ... parameters

If f is variadic with final parameter type ...T, then within the function the argument is equivalent to a parameter of type []T. At each call of f, the argument passed to the final parameter is a new slice of type []T whose successive elements are the actual arguments, which all must be assignable to the type T. The length of the slice is therefore the number of arguments bound to the final parameter and may differ for each call site.


Package exec

func Command

func Command(name string, arg ...string) *Cmd

Command returns the Cmd struct to execute the named program with the given arguments.

The returned Cmd's Args field is constructed from the command name followed by the elements of arg, so arg should not include the command name itself. For example, Command("echo", "hello")


For example,

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    name := "echo"
    args := []string{"hello", "world"}
    cmd := exec.Command(name, args...)
    out, err := cmd.Output()
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(string(out))
}

Output:

hello world

How can I pass a slice as a variadic input?, Variadic functions can be called with any number of trailing arguments. already have multiple args in a slice, apply them to a variadic function using func(slice. dot dot dot () in final parameter of Go function means you can pass zero, one, or more arguments for that parameter according to Function types in Go spec, and the function of such kind is usually called variadic function.

A list of command arguments can be retrieved from the flag package Args() function. You can then pass this to a function using the variadic input style (func(input...))

From the Spec:

If f is variadic with final parameter type ...T, then within the function the argument is equivalent to a parameter of type []T. At each call of f, the argument passed to the final parameter is a new slice of type []T whose successive elements are the actual arguments, which all must be assignable to the type T.

Example:

package main

import "fmt"

func echo(strings ...string) {
    for _, s := range strings {
        fmt.Println(s)
    }
}

func main() {
    strings := []string{"a", "b", "c"}
    echo(strings...) // Treat input to function as variadic
}

See The Go spec for more details.

Playground

Variadic Functions, Pass slice or array as variadic () parameter in Go programming language. There is a syntactic sugar which can be used to pass a slice to a variadic function. You have to suffix the slice with ellipsis If that is done, the slice is directly passed to the function without a new slice being created.

func Command

func Command(name string, arg ...string) *Cmd

Command returns the Cmd struct to execute the named program with the given arguments.

So you have to extract the command which is found at sliceA[0] and then pass all the arguments with a variadic but removing the command sliceA[1:]....

import "os/exec"
import "strings"
func main(){
    plainCommand  := "echo hello world"
    sliceA := strings.Fields(plainCommand)
    cmd := exec.Command(sliceA[0], sliceA[1:]...)
}

[Golang] Pass Slice or Array to Variadic Function, Learn how to create variadic functions in Go with examples. We will write a simple program to find whether an integer exists in an input list of integers. Let's pass a slice to a variadic function and find out what happens from  You can also pass an existing slice in a variadic function. To do this, we pass a slice of the complete array to the function as shown in Example 2 below. When you do not pass any argument in the variadic function, then the silce inside the function is nil. The variadic functions are generally used for string formatting. You can also pass multiple slice in the variadic function.

Go Variadic Functions Tutorial with Examples, I'm wondering if it's possible to use a slice to populate a list of arguments . I'm trying to solve the following program. Basically to mimic a  Appending items to a variadic function wrapper without reallocating a new slice. How can I pass a slice as a variadic input? 0. Variadic function wrapper. 0.

How to pass a slice as a variadic input?, Println function and pass in zero, one, or more values: print.go This is because the variadic parameter was an empty slice of string . Since we are When the number of input params are unknown or will vary when called. ) no new slice is created, the one you pass is used as is. And the passed slice can only be used to specify the value of one – the final – variadic parameter. Attempting to pass both a single element and a slice will not match the signature (the parameter list in this case) of your function and you'll get an error: too many arguments in call to foo

How To Use Variadic Functions in Go, Hence, all arguments passed to this function is stored in a elem slice. Let's take an example of append function. append([]Type, args, arg2, argsN). append  A variadic param gets converted to a “new” slice inside the func. A variadic param is actually syntactic sugar for an input parameter of a slice type. A variadic param becomes a nil slice inside the func when you don’t pass any values to it. All non-empty slices have associated underlying arrays.