英文:
"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)\\`"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论