Created
March 18, 2016 20:56
-
-
Save conoro/8ae506e8e848435c145b to your computer and use it in GitHub Desktop.
How to see actual command errors in Go when shelling out
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 ( | |
"bytes" | |
"fmt" | |
"os/exec" | |
) | |
func main() { | |
srcFile := "file.mp3" | |
destFolder := "D:\\mp3s\\" | |
cpCmd := exec.Command("cmd.exe", "/c", "copy", srcFile, destFolder) | |
var out bytes.Buffer | |
var stderr bytes.Buffer | |
cpCmd.Stdout = &out | |
cpCmd.Stderr = &stderr | |
err := cpCmd.Run() | |
if err != nil { | |
fmt.Println(fmt.Sprint(err) + ": " + stderr.String()) | |
fmt.Println("Result: " + out.String()) | |
return | |
} | |
fmt.Println("Result: " + out.String()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment