Skip to content

Instantly share code, notes, and snippets.

@willthames
Created December 27, 2023 23:22
Show Gist options
  • Save willthames/7de2f30e0c8a3a77e7be501cad625851 to your computer and use it in GitHub Desktop.
Save willthames/7de2f30e0c8a3a77e7be501cad625851 to your computer and use it in GitHub Desktop.
go error handling
package main
import (
"log"
"os/exec"
)
func main() {
cmd := exec.Command("./error.sh")
log.Printf("Running command and waiting for it to finish...")
out, err := cmd.Output()
if out != nil {
log.Printf("output %v", string(out))
}
if ee, ok := err.(*exec.ExitError); ok {
log.Printf("error %v exit status %d", string(ee.Stderr), ee.ExitCode())
}
}
#!/bin/sh
echo "This is stdout"
echo "This is stderr" >& 2
exit 1
module example.com/cmd
go 1.21.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment