package main import ( "fmt" "os/exec" ) func main() { cmd := exec.Command("echo", "hello world") out, err := cmd.StdoutPipe() if err != nil { panic(err) } if err := cmd.Start(); err != nil { panic(err) } buf := make([]byte, 1024) n, err := out.Read(buf) if err != nil { panic(err) } fmt.Println(string(buf[0:n])) }
package main import ( "fmt" "os/exec" ) func main() { cmd := exec.Command("ls", "-l") out, err := cmd.StdoutPipe() if err != nil { panic(err) } if err := cmd.Start(); err != nil { panic(err) } buf := make([]byte, 1024) n, err := out.Read(buf) if err != nil { panic(err) } fmt.Println(string(buf[0:n])) }Both examples use the os/exec package in Golang.