How to Execute a Shell Command in Go
Created
Modified
Using exec.Command Function
Command returns the Cmd struct to execute the named program with the given arguments.
You can execute a shell command using the exec.Command() function. For example,
package main
import (
"bytes"
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("ls", "-lh")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
// log.Fatal(err)
}
fmt.Println(out.String())
}
total 188K -rw-r--r--...