Handling Output from External Programs

Overview

The two external programs we’ve executed are well-behaved; if something goes wrong, they return an error that we can capture and use to make decisions. Unfortunately, not all programs work like that.

In some cases, the program exits with a successful return code even when something goes wrong. In these cases a message in STDOUT or STDERR generally provides details about the error condition. In other cases, a program completes successfully as designed but something on its output tells us that the condition represents an error.

When executing external programs in Go, we can handle both of these scenarios by capturing the program’s output and parsing it to make decisions.

Get hands-on with 1400+ tech skills courses.