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 關注
- 243 瀏覽
添加回答
舉報