1 回答
TA贡献1807条经验 获得超9个赞
您可以使用 json。元帅缩进:
out, err := json.MarshalIndent(json.RawMessage(jsonData), "", " ")
if err != nil {
panic(err)
}
cmd := exec.Command("echo", string(out))
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
panic(err)
}
https://play.golang.org/p/J4Z5txNOmgP
根据实现方式,您可能需要转义并显式引用格式化的 json 字符串。session2.Run
// this is a naive single quote bash-specific escape, if this isn't
// sufficient for your use case you'll have to improve on it.
jsonData = strings.ReplaceAll(string(out), "'", `'"'"'`)
cmd := fmt.Sprintf("echo '%s' >> %s", jsonData, tmpFileName)
if err := session2.Run(cmd); err != nil {
panic(err)
}
- 1 回答
- 0 关注
- 305 浏览
添加回答
举报
