dougou6727
2019-02-15 15:15
浏览 802
已采纳

在Go中转义反引号(exec.Command中使用的字符串)

Given this code, I can't figure out how to escape the backtick.

var (
    MY_STRING = "something`something"
)
cmd := fmt.Sprintf("MY_ENV=%q;", MY_STRING)
out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
// results in MY_ENV="something`something" ie unfinished input

I've tried the below but it results in "unknown escape sequence". It does work in the shell obviously. I've also tried to combine strings and raw string literals but with no success. How can I escape the backtick please?

var (
    MY_STRING = "something\`something"
)

图片转代码服务由CSDN问答提供 功能建议

给出此代码,我不知道如何转义反引号。

  var(
 MY_STRING =“某物” 
)
cmd:= fmt.Sprintf(“ MY_ENV =%q;”,MY_STRING)
out,err:= exec.Command(“ bash  “,” -c“,cmd).CombinedOutput()
 //导致MY_ENV =” something`something“,即未完成的输入
   
 
 

我尝试过 以下内容,但会导致“未知的转义序列”。 显然,它确实在外壳中工作。 我也尝试将字符串和原始字符串文字结合起来,但是没有成功。

  var(
 MY_STRING =“ something \`something” 
)
   
 <  / DIV>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题