package main import ( "fmt" "os/exec" ) func main() { cmd := exec.Command("ls") out, err := cmd.Output() if err != nil { fmt.Println(err) } fmt.Println(string(out)) }
package main import ( "fmt" "os/exec" ) func main() { cmd := exec.Command("echo", "hello", "world") out, err := cmd.Output() if err != nil { fmt.Println(err) } fmt.Println(string(out)) }This example creates a "Cmd" struct for the "echo" command with two arguments and executes it using the "Output" method. The output of the command is captured as a byte array and is printed to the console as a string. These examples demonstrate the usage of the "os/exec" package library in Go for executing external commands and capturing their output.