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
+36.7k Golang : Display float in 2 decimal points and rounding up or down
+12.2k Golang : Get remaining text such as id or filename after last segment in URL path
+12.7k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+19.8k Golang : Append content to a file
+8.2k Golang : Qt splash screen with delay example
+9.4k Golang : Apply Histogram Equalization to color images
+13.7k Golang : Check if an integer is negative or positive
+21.9k Golang : Use TLS version 1.2 and enforce server security configuration over client
+9.2k Golang : Write multiple lines or divide string into multiple lines
+5.8k Golang : List all packages and search for certain package
+20.7k Android Studio : AlertDialog and EditText to get user string input example