Cmd.CombinedOutput in Go

Posted by GoDoc
Public (Editable by Users)

CombinedOutput runs the command and returns its combined standard output and standard error.

Go
Edit
package main

import (
	"fmt"
	"log"
	"os/exec"
)

func main() {
	cmd := exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr")
	stdoutStderr, err := cmd.CombinedOutput()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", stdoutStderr)
}