Can we convert a line into slices of words?

Hello, as we know golang doesn’t support long Linux commands like service httpd restart
we have to run it via exec.Command(“service”,“https”,“restart”).Output()

What am thinking is to create an function called shell_exec() which takes an string argument and convert it to slices by space and we can run the command via exec.Command
Like shell_exec("service httpd restart")
then shell_exec function will convert sercice httpd restart to three different words service httpd restart
and then we can able to send these words to exec.Command().Output()

Anyone have any idea ?

Hi there,
you can use strings.Split function

// command - a parameter of shell_exec
args := strings.Split(command, " ")

In VS Code , at ... i’m getting this error - can only use ... with matching parameter compiler
and when using go build , i’m getting this error :

# command-line-arguments
./gocmd.go:14:27: not enough arguments in call to exec.Command
        have ([]string...)
        want (string, ...string)

To match the function signature, it needs to be written as

exec.Command(args[0], args[1:]...)
1 Like