为了账号安全,请及时绑定邮箱和手机立即绑定

如何阻止 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='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. 如果我将代码更改为这样(移动单词 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 关注
  • 86 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信