本文详解为何 `exec.command("go run file.go")` 会报错“executable file not found”,并提供正确调用方式、路径处理建议及安全注意事项。
在 Go Web 开发中,若希望动态执行用户提交的 Go 源码(如构建简易在线 Playground),常会借助 os/exec 包调用 go run。但初学者易犯一个关键错误:将完整命令字符串(如 "go run main.go")直接传入 exec.Command(),导致运行时抛出:
exec: "go run testcode.go": executable file not found in $PATH
这是因为 exec.Command(name, args...) 的第一个参数必须是可执行文件的路径(如 go),后续所有参数需单独作为 string 类型传递,而非拼接成单个字符串。系统不会解析空格分隔的命令行——它只会尝试在 $PATH 中查找名为 "go run testcode.go" 的可执行文件(显然不存在)。
✅ 正确写法如下:
cmd := exec.Command("go", "run", title+".go")
out, err := cmd.Output()注意:
goPath, err := exec.LookPath("go")
if err != nil {
http.Error(w, "Go compiler not found", http.StatusInternalServerError)
return
}
cmd := exec.Command(goPath, "run", title+".go")⚠️ 安全与可靠性注
意事项:
matched := regexp.MustCompile(`^[a-zA-Z0-9_-]+$`).MatchString(title)
if !matched {
http.Error(w, "Invalid title", http.StatusBadRequest)
return
}ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() cmd := exec.CommandContext(ctx, "go", "run", title+".go")
最后,http.StatusFound(即 HTTP 302)是重定向状态码,表示“目标资源临时位于其他 URI”,浏览器会自动跳转——这正是 /save/ 处理后跳转至 /exec/ 的标准做法。
综上,修正 exec.Command 的参数拆分方式,并辅以路径校验、超时和上下文管理,即可安全、可靠地实现 Go 代码的动态执行功能。