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
+7.3k Golang : Not able to grep log.Println() output
+13.4k Golang : Verify token from Google Authenticator App
+9.4k Golang : Web(Javascript) to server-side websocket example
+8.4k Golang : Ackermann function example
+27.5k Golang : dial tcp: too many colons in address
+9.3k Golang : Temperatures conversion example
+17.6k Convert JSON to CSV in Golang
+28k Golang : Move file to another directory
+7.7k Golang : Command line ticker to show work in progress
+8.2k How to show different content from website server when AdBlock is detected?
+12.3k Golang : Display list of countries and ISO codes