How can I use exec with multiple argument in go? I've found an example of exec in go, but i don't understand how to use multiple args, this is what i've tried:
cmdName := os.Args[1]
cmdArgs := []string{os.Args[1:]}
cmd := exec.Command(cmdName, cmdArgs...)
Here is an example how to use exec
with multiple arguments:
package main
import (
"fmt"
"log"
"os"
"os/exec"
)
func main() {
args := os.Args[1:]
fmt.Printf("%s
", args)
res, err := exec.Command("ls", args...).Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%q
", string(res))
}
Without args:
echo -e $(go run args.go )
[] "args.go
file1
file2
file3
"
With -s
and -r
args:
echo -e $(go run args.go -s -r )
[-s -r] "total 4
0 file3
0 file2
0 file1
4 args.go
"