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
+19.9k Golang : Measure http.Get() execution time
+20k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+31.6k Golang : Get local IP and MAC address
+10k Golang : Convert octal value to string to deal with leading zero problem
+9.4k Facebook : Getting the friends list with PHP return JSON format
+15.2k Golang : Get HTTP protocol version example
+4.8k Which content-type(MIME type) to use for JSON data
+9.3k Golang : How to get username from email address
+6.9k Mac/Linux/Windows : Get CPU information from command line
+19.3k Golang : Get RGBA values of each image pixel
+17k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+13.9k Golang : Get current time