package main import ( "fmt" "os/exec" ) func main() { cmd := exec.Command("ls") cmd.Dir = "/home/user" out, err := cmd.Output() if err != nil { fmt.Println(err.Error()) return } fmt.Println(string(out)) }
package main import ( "fmt" "os/exec" ) func main() { cmd := exec.Command("mkdir", "test") cmd.Dir = "/home/user" err := cmd.Run() if err != nil { fmt.Println(err.Error()) return } fmt.Println("Command executed successfully.") }In this example, we create a new Cmd object to execute the "mkdir" command with argument "test". We set the working directory to "/home/user" using Dir. Then we run the command using Run(), which blocks until the command completes. If there is an error, we print it to the console. Otherwise, we print a success message. The os/exec package is part of the standard library for Go.