Last Updated - 6th December, 2018
In this tutorial, we are going to be taking a look at the
os/exec package in the standard library and how we can use this to successfully execute system commands within our Go applications.
Note - The official documentation for executing system commands can be found under the exec package: os/exec package.
Cross Compatibility Issues
Please note that some of these commands may not work on your operating system. If you are trying to write code that is compatible on multiple platforms then it would be wise to select commands that only feature on all platforms. If this is un-achievable then I recommend you add conditional logic to your program that executes a different system command depending on the system it’s executing on top of.
Checking Current Operating System
In order to check what operating system our code is running on we can use the runtime package and check the GOOS constant. This will return the operating system target:
Note - The full list of GOOS variables can be found here: Sys Package.
Note - I’m writing this tutorial on MacOS using commands that may not necessarily work on Windows machines.
Let’s have a look at how we can start executing some really simple commands such as
pwd using the
os/exec package, and once we have the basics covered, we can move on to more advanced examples.
We’ll first of all need to import the 3 key packages for this example, the
os/exec and the
Once, we’ve done this, we’ll define an
execute() function which will attempt to execute the
If we then attempt to run this, we should see the following:
As you can see, both of the commands are successfully executed and we’ve managed to capture the output from these commands and subsequently output them within the context of our own Go program.
Passing in Arguments
Awesome, we’ve managed to get some really simple commands running, but how do we go about passing in arguments to these commands?
For instance, say I wanted to do an
ls -ltr as opposed to just a standard
Thankfully, this is relatively easy, we just have to add these arguments to
.Command() like so:
When we go to execute this again, we should see the output now successfully picking up our
.Command()is an example of a Variadic Function which takes in any number of trailing arguments, therefore, you can pass in as many arguments to your initial command as you desire.
So, in this tutorial, we looked at how we could leverage the
os/exec package in Go to execute system commands within our Go programs.
If you found this tutorial useful or wish to ask anything extra then please feel free to do so in the comments section below!
Note - If you want to keep up to date with the latest articles and updates on the site then please feel free to follow me on twitter: @Elliot_f