Golang os/exec.Cmd.CombinedOutput() function example

package os

Golang os/exec.Cmd.CombinedOutput() function usage example

 package main

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

 func main() {

  cmd := exec.Command("/bin/echo", "Hello World!")

  // Join the output but without error yet
  fmt.Printf("Output : %s \n", strings.Join(cmd.Args, " "))

  //output, err := cmd.CombinedOutput() // combine the output with standard error
  _, err := cmd.CombinedOutput() // combine the output with standard error

  fmt.Printf("Combined output :  %s \n", err)

 }

Sample output :

Output : /bin/echo Hello World!

Combined output : fork/exec /bin/echo: not implemented on Native Client

Reference :

http://golang.org/pkg/os/exec/#Cmd.CombinedOutput

Advertisement