Running external programs
There are many use cases where you want to execute an external program to perform a task. Usually, this is because performing the same task within your own program is not possible or not easy. For example, you may choose to execute several instances of an external image processing program to modify a group of images. Another use case is when you want to configure some device using programs provided by its manufacturer. This recipe includes several ways to execute external programs.
How to do it...
Use exec.Command
or exec.CommandContext
to run another program from your program. exec.Command
is appropriate if you do not need to cancel (kill) the child process or impose a timeout. Otherwise, use exec.CommandContext
, and cancel or time out the context to kill the child process:
- Create the
exec.Command
(orexec.CommandContext
) object using the name of the program and its arguments:- If you need to search the program in the platform’s executable...