且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Golang 中执行一个简单的 Windows 命令?

更新时间:2023-09-30 13:17:58

我遇到了和你一样的错误.但是dystroy 是正确的:你不能运行delcmd 内置的任何其他命令,因为没有del.exe 文件(或任何与此相关的其他删除可执行文件).

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).

我可以使用它:

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)
    }   
}