Go exec.CommandThe Command returns the Cmd struct to execute the specified program with the given arguments. The first parameter is the program to be run; the other arguments are parameters to the program.
Example of Go Exec.Command
In the example, we transform the input via the tr command.
The tr standard Linux command translates, squeezes, and/or deletes characters from standard input, writing to standard output. In our case, we transform lowercase letters to uppercase ones.
Through the Stdin field, we pass a string to the command as its input.
The output of the program will be written to the bytes buffer.
Go exec command with multiple argsWe can pass multiple arguments to the exec.Command.
The example runs the echo command with three arguments.
Go exec command capture outputThe Output runs the command and returns its standard output.
The example captures the output of the ls command and prints it.
In this page (written and validated by A. Gawali) you learned about Go Exec Command . What's Next? If you are interested in completing Go tutorial, your next topic will be learning about: Go Pointers.
Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: firstname.lastname@example.org.