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
+55.3k Golang : Unmarshal JSON from http response
+13.7k Golang : Check if an integer is negative or positive
+18.9k Golang : Read input from console line
+12.6k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+4.8k Golang : A program that contain another program and executes it during run-time
+4.9k Unix/Linux : secure copying between servers with SCP command examples
+14.4k Golang : How to pass map to html template and access the map's elements
+10.2k Golang : How to get quoted string into another string?
+17.7k Golang : [json: cannot unmarshal object into Go value of type]
+22.2k Golang : How to run Golang application such as web server in the background or as daemon?
+11.8k Golang : convert(cast) float to string
+10.9k Golang : Removes punctuation or defined delimiter from the user's input