如何在Golang中执行一个简单的Windows DOS命令? [英] How to execute a simple Windows DOS command in Golang?
问题描述
有人可以这么亲切告诉我如何在Go中运行一个简单的DOS命令吗?
当运行这样的东西时:
exec.Command del,c:\\aaa.txt)
输出为: p>
del:在%path%中找不到可执行文件
再次,我认为这将是简单的任务,但经过2小时的搜索,我可以找不到这个的例子。有人可以告诉我我做错了什么,因为我找不到在Go最简单的常见任务的好例子。
我有和你一样的错误。
但是dystroy是正确的:你不能运行 del
或任何其他命令内置到 cmd
是没有 del.exe
文件(或任何其他del-executable可执行文件)。
使用:
package main
import(
fmt
os / exec
)
func main(){
c:= exec.Command(cmd,/ C,del,D: \\\a.txt)
如果err:= c.Run(); err!= nil {
fmt.Println(Error:,err)
}
}
Can someone be so kind to tell me how to run a simple DOS command in Go?
When running something like this:
exec.Command("del", "c:\\aaa.txt")
The output is:
"del: executable file not found in %path%"
Again I thought this would be simple task in Go, but after 2 hours of searching, I could find no examples of this. Can someone also tell me what I'm doing wrong, because I cannot find good examples on the simplest common tasks in Go.
I got the same error as you.
But dystroy is correct: You can't run del
or any other command built into cmd
because there is no del.exe
file (or any other del-executable for that matter).
I got it to work with:
package main
import(
"fmt"
"os/exec"
)
func main(){
c := exec.Command("cmd", "/C", "del", "D:\\a.txt")
if err := c.Run(); err != nil {
fmt.Println("Error: ", err)
}
}
这篇关于如何在Golang中执行一个简单的Windows DOS命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!