“go generate” 中的 “bad quoted string” 在 Go 语言中是一个错误的引用字符串。

huangapple go评论157阅读模式
英文:

"bad quoted string" in go generate

问题

我正在尝试使用go generate将一个文件插入到一个go文件中作为变量,但是它报错了,显示"bad quoted string"。问题是,我可以在shell中正常运行这个命令:

//go:generate echo "var baseConfigProduction = \`" && cat base-production.json && echo "\`"

我在这里漏掉了什么?

英文:

I'm trying to insert a file as a variable in a go file using go generate but it fails with bad quoted string, the issue is that I can run this in a shell without issue:

//go:generate echo "var baseConfigProduction = \`" && cat base-production.json && echo "\`"

What am I missing here?

答案1

得分: 2

只有调用一个命令,Go generate 才能工作。如果你想调用多个命令,你可以将它们放入一个 BASH 脚本中,或者像这样做:

//go:generate echo var baseConfigProduction = `
//go:generate cat base-production.json
//go:generate echo `

但据我所知,对于多个 go:generate 命令的评估顺序是未定义的,所以你不能真正依赖它,所以我建议使用 BASH 脚本。

**编辑:**另一种可能性:

//go:generate sh -c "echo var baseConfigProduction = \`$DOLLAR(cat base-production.json)\`"
英文:

Go generate only works by calling one command. If you want to call several, you either put them into a BASH script, or do something like this:

//go:generate echo var baseConfigProduction = `
//go:generate cat base-production.json
//go:generate echo `

But AFAIK, the order of evaluation for several go:generate commands is undefined, so you can't really count on that, so I would recommend sticking to BASH scripts.

EDIT: Another possibility:

//go:generate sh -c "echo var baseConfigProduction = \\`$DOLLAR(cat base-production.json)\\`"

huangapple
  • 本文由 发表于 2016年12月14日 20:12:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/41142354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定