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 :
Advertisement
Something interesting
Tutorials
+8.9k Golang : GMail API create and send draft with simple upload attachment example
+37.5k Upload multiple files with Go
+9.2k Golang : does not implement flag.Value (missing Set method)
+14.6k Golang : Send email with attachment(RFC2822) using Gmail API example
+8.1k Golang : Multiplexer with net/http and map
+14.6k Golang : Execute function at intervals or after some delay
+12.6k Golang : flag provided but not defined error
+19.2k Golang : Check if directory exist and create if does not exist
+6k Golang : How to verify input is rune?
+5.9k Golang : Generate multiplication table from an integer example
+8.3k Useful methods to access blocked websites
+15.3k Golang : Get all local users and print out their home directory, description and group id