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
+9.8k Golang : Resumable upload to Google Drive(RESTful) example
+14.5k Golang : How to check if your program is running in a terminal
+18.2k Golang : Put UTF8 text on OpenCV video capture image frame
+5.4k How to check with curl if my website or the asset is gzipped ?
+11.3k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+15.6k Golang : ROT47 (Caesar cipher by 47 characters) example
+11.7k Golang : Calculations using complex numbers example
+13.6k Golang : Query string with space symbol %20 in between
+15.7k Golang : Get checkbox or extract multipart form data value example
+5.4k Javascript : How to loop over and parse JSON data?
+30.8k Golang : Download file example
+51.4k Golang : Check if item is in slice/array