我正在用 Golang 編寫一個程序,它將使用 Mozilla 的 Thunderbird 電子郵件客戶端發送電子郵件。應執行的 Windows 命令是: start "" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe" -compose "to='[email protected]',subject='Subject1',body='Hello'" -offline我的 Go 代碼如下所示(命令是上面列出的命令): var command string command = `start "" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"` command += ` -compose "to='` + toAddress + `',` command += `subject='Subject1',` command += `body='Hello'"` command += ` -offline` cmd := exec.Command("cmd.exe", "/C", command)但我收到一個錯誤:Windows cannot find '\\'. Make sure you typed the name correctly, and then try again. 如果我將代碼更改為這樣(移動單詞 start): var command string command = ` "" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"` command += ` -compose "to='` + toAddress + `',` command += `subject='Subject1',` command += `body='Hello'"` command += ` -offline` fmt.Println("Command: " + command) cmd := exec.Command("cmd.exe", "/C", "start", command)然后我得到另一個錯誤:Windows cannot find 'Files'. Make sure you typed the name correctly, and then try again. 似乎不是嘗試啟動“”,而是嘗試啟動 \\。如何保留雙引號?
1 回答

嗶嗶one
TA貢獻1854條經驗 獲得超8個贊
您的問題可能是傳遞給的每個單獨的字符串都exec.Command
作為單個參數傳遞(不解析它)cmd.exe
,這可能也不會拆分給定的字符串,因此您必須自己執行此操作。
其中參數也被拆分。您應該能夠省略 " ,因為無論如何您都手動將其拆分,或者為它編寫一個程序或使用執行拆分的解釋器運行它。
func do() {
? ? args := []string{
? ? ? ? "/C",
? ? ? ? "start",
? ? ? ? "",
? ? ? ? `C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe`,
? ? ? ? "-compose",
? ? ? ? "to=" + toAddress + ",subject=Subject1,body=Hello",
? ? ? ? "-offline",
? ? }
? ? cmd := exec.Command("cmd.exe", args...)
}
- 1 回答
- 0 關注
- 194 瀏覽
添加回答
舉報
0/150
提交
取消