I want my method to get the exec command as a string. If the input string has spaces, how to split it into cmd, args for os.exec?
The documentation says about creating my Exec.Cmd structure, e.g.
cmd := exec.Command("tr", "a-z", "A-Z")
This works great:
a := string("ifconfig")
cmd := exec.Command(a)
output, err := cmd.CombinedOutput()
fmt.Println(output) // prints ifconfig output
This fails:
a := string("ifconfig -a")
cmd := exec.Command(a)
output, err := cmd.CombinedOutput()
fmt.Println(output) // 'ifconfig -a' not found
I tried strings.Split (a) but got an error: I can not use (type [] string) as a type string in the argument to exec.Command
source
share