我有这段代码:
func RunCmd(cmdString string) {
cmd := exec.Command(cmdString)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr)
}
然后我尝试使用:
RunCmd("echo abc")
但是如果失败并告诉我:
cmd.Run() failed with exec: "echo abc": executable file not found in %PATH%
我也尝试了“ dir”命令,仍然是同样的错误。那么我在哪里弄错了以及如何解决呢?