Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a slice as a variadic input?

Tags:

go

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)     } 
like image 828
The user with no hat Avatar asked May 18 '14 16:05

The user with no hat


People also ask

What is go variadic function?

A variadic function is a function that accepts a variable number of arguments. In Golang, it is possible to pass a varying number of arguments of the same type as referenced in the function signature.

Can variadic methods take any number of parameters?

A variadic function allows you to accept any arbitrary number of arguments in a function.

What symbols are used in a function declaration to indicate that it is a variadic function?

A function with a parameter that is preceded with a set of ellipses ( ... ) is considered a variadic function. The ellipsis means that the parameter provided can be zero, one, or more values. For the fmt. Println package, it is stating that the parameter a is variadic.


2 Answers

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 
like image 152
peterSO Avatar answered Oct 08 '22 09:10

peterSO


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

like image 30
Intermernet Avatar answered Oct 08 '22 08:10

Intermernet