[英]How to stop Golang from replacing double-quotes with backslashes when executing a Windows command?
我正在用 Golang 编写一个程序,它将使用 Mozilla 的 Thunderbird 电子邮件客户端发送电子邮件。 应该执行的 Windows 命令是:
start "" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe" -compose "to='CloudCoin@Protonmail.com',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.
如果我将代码更改为此(移动单词开始):
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.
似乎不是尝试启动“”,而是尝试启动 \\\\。 我怎样才能保留我的双引号?
您的问题可能是传递给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...)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.