package main
import (
"fmt"
"os/exec"
)
func main() {
err := exec.Command("pwd").Run()
if err != nil {
fmt.Println("Command Exec Error.")
}
}
package main
import (
"fmt"
"os/exec"
)
func main() {
out, err := exec.Command("pwd").Output()
if err != nil {
fmt.Println("Command Exec Error.")
}
//Output the result of the executed command
fmt.Printf("pwd result: %s", string(out))
}
- CombinedOutput
package main
import (
"fmt"
"os/exec"
)
func main() {
out, err := exec.Command("ls", "dummy").CombinedOutput()
if err != nil {
fmt.Println("Command Exec Error.")
}
//Contents of standard output + standard error output of the executed command
fmt.Printf("ls result: \n%s", string(out))
}
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("sleep", "10")
fmt.Println("Command Start.")
err := cmd.Start()
if err != nil {
fmt.Println("Command Exec Error.")
}
//Wait when uncommented
//cmd.Wait()
fmt.Println("Command Exit.")
}
https://blog.y-yuki.net/entry/2017/04/28/000000
Recommended Posts