亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何阻止 Golang 在執行 Windows 命令時用反斜杠替換雙引號?

如何阻止 Golang 在執行 Windows 命令時用反斜杠替換雙引號?

Go
互換的青春 2023-07-10 10:47:03
我正在用 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...)

}


查看完整回答
反對 回復 2023-07-10
  • 1 回答
  • 0 關注
  • 194 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號