liveBook · Manning

Os/exec package

As explained in Go in Practice, Second Edition

The Go os/exec package runs external operating-system commands and provides access to their input, output, and termination status.

The os/exec package lets a Go application call another operating-system application. In varied cloud environments, validate each external dependency before its first use rather than assuming it is installed. section 12.4.1

Check a dependency before calling it

func checkDep(name string) error { if _, err := exec.LookPath(name); err != nil { return fmt.Errorf("Could not find '%s' in PATH: %s", name, err) } return nil } err := checkDep("fortune") if err != nil { log.Fatalln(err) } fmt.Println("Time to get your fortune") LookPath searches PATH and returns an error when the named application is unavailable. Logging that error makes deployment failures easier to troubleshoot. section 12.4.1

How Go in Practice, Second Edition covers it

How deep it goes
Defines the dependency-checking problem and implements LookPath-based validation, without detailing command execution internals.
Examples
Uses a checkDep helper to verify the fortune application and report a missing executable.
What you can do after
Diagnose missing external applications before invocation and produce a reportable PATH-related error.
How it connects
Connects external applications with PATH lookup, runtime environment detection, cloud deployment, and error logging.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.