Executing External Programs
Learn how to execute external programs using Go.
We'll cover the following...
Overview
So far, we’ve developed several command-line tools with Go by executing tasks with our own algorithms. In some situations, it’s easier to delegate some of these tasks to more specialized programs that are already available on our system. For example, we might want to use Git to execute version control commands against a Git repository or launch Firefox to display a web page on a browser.
In some cases, these specialized programs have an API available that we can call directly from our program. When this isn’t available, we have to use their functionality by executing external commands from our Go program.
Go provides some lower-level libraries, such as syscall
, but unless we have specific requirements, it’s best to use the higher-level interface provided by the os/exec
package.
What will we learn?
In this chapter, we’ll apply the os/exec
package to develop a simple, but useful,
implementation of a Continuous Integration (CI) tool for our Go programs.
A typical CI pipeline consists of several automated steps that continuously
ensure a code base or an application is ready to be merged with some other
developer’s code, usually in a shared version control repository.
For this example, the CI pipeline consists of:
- Building the program using
go build
to verify that the program