Created
November 27, 2019 09:42
-
-
Save kjk/ab62b7fa99b0525dd424021a785753d7 to your computer and use it in GitHub Desktop.
more advanced command execution https://www.notion.so/essentialbooks/Executing-commands-c290f0566c80467a9005ab3a4024ec1d (made with https://codeeval.dev)
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" | |
"log" | |
"os/exec" | |
) | |
func main() { | |
// :show start | |
var stdout, stderr bytes.Buffer | |
cmd := exec.Command("go", "version") | |
cmd.Stdout = &stdout | |
cmd.Stderr = &stderr | |
err := cmd.Start() | |
if err != nil { | |
log.Fatalf("cmd.Start() failed with '%s'\n", err) | |
} | |
err = cmd.Wait() | |
if err != nil { | |
log.Fatalf("cmd.Wait() failed with '%s'\n", err) | |
} | |
out := append(stdout.Bytes(), stderr.Bytes()...) | |
fmt.Printf("Output:\n%s\n", string(out)) | |
// :show end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment