How to execute a simple Windows command in Golang?

cmd, go, windows

Solution

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() {
    var c *exec.Cmd

    switch runtime.GOOS{
    case "windows":
        c = exec.Command("cmd", "/C", "del", "D:\\a.txt")

    default://Mac & Linux
        c = exec.Command("rm", "-f", "/d/a.txt")
    }

    if err := c.Run(); err != nil { 
        fmt.Println("Error: ", err)
    }
}

Problem

How to run a simple Windows command? This command: ``` exec.Command("del", "c:\\aaa.txt") ``` .. outputs this message: del: executable file not found in %path% What am I doing wrong?

Original source