package main import ( "fmt" "os/exec" ) func main() { cmd := exec.Command("ls", "-l") output, err := cmd.Output() if err != nil { fmt.Println("Error:", err) return } fmt.Println(string(output)) }
package main import ( "fmt" "os/exec" ) func main() { cmd := exec.Command("cat") stdin, err := cmd.StdinPipe() if err != nil { fmt.Println("Error:", err) return } stdout, err := cmd.StdoutPipe() if err != nil { fmt.Println("Error:", err) return } err = cmd.Start() if err != nil { fmt.Println("Error:", err) return } fmt.Fprint(stdin, "Hello, World!") stdin.Close() buf := make([]byte, 512) n, err := stdout.Read(buf) if err != nil { fmt.Println("Error:", err) return } fmt.Println(string(buf[:n])) err = cmd.Wait() if err != nil { fmt.Println("Error:", err) return } }This code uses the `Command` method of the `os/exec` package to create a new process to execute the `cat` command. The `StdinPipe` and `StdoutPipe` methods are then used to get a handle on the process's input and output streams, respectively. The `Start` method is then used to start the process. The code then writes `"Hello, World!"` to the process's input stream using the `Fprint` method of the `fmt` package. It then reads from the process's output stream using the `Read` method of the `stdout` pipe. The output is printed to the console, and the `Wait` method is used to wait for the process to exit.