Created
December 27, 2023 23:22
-
-
Save willthames/7de2f30e0c8a3a77e7be501cad625851 to your computer and use it in GitHub Desktop.
go error handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
echo "This is stdout" | |
echo "This is stderr" >& 2 | |
exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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