package main import ( "fmt" "os/exec" ) func main() { // Execute the "ls" command and get the output cmd := exec.Command("ls") output, err := cmd.CombinedOutput() if err != nil { fmt.Println(err) return } fmt.Println(string(output)) }
package main import ( "fmt" "os/exec" ) func main() { // Execute the "echo" command with argument and get the output cmd := exec.Command("echo", "Hello, World!") output, err := cmd.CombinedOutput() if err != nil { fmt.Println(err) return } fmt.Println(string(output)) }In this example, we are using the os/exec package to execute the "echo" command with an argument and get the output as a byte slice. The CombinedOutput method is used to get the combined stdout and stderr output. We are converting the output to a string and printing it to the console. Package: os/exec.