GO語言"encoding/json"包中"Indent"函數的用法及代碼示例。
用法:
func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error
Indent 將 JSON-encoded src 的縮進形式附加到 dst。 JSON 對象或數組中的每個元素都從一個新的縮進行開始,該行以前綴開頭,後跟根據縮進嵌套的一個或多個縮進副本。附加到 dst 的數據不以前綴或任何縮進開頭,以便更容易嵌入到其他格式化的 JSON 數據中。盡管 src 開頭的前導空格字符(空格、製表符、回車、換行符)被刪除,但 src 末尾的尾隨空格字符被保留並複製到 dst。例如,如果 src 沒有尾隨空格,則 dst 也不會;如果 src 以尾隨換行符結尾,則 dst 也是如此。
例子:
package main
import (
"bytes"
"encoding/json"
"log"
"os"
)
func main() {
type Road struct {
Name string
Number int
}
roads := []Road{
{"Diamond Fork", 29},
{"Sheep Creek", 51},
}
b, err := json.Marshal(roads)
if err != nil {
log.Fatal(err)
}
var out bytes.Buffer
json.Indent(&out, b, "=", "\t")
out.WriteTo(os.Stdout)
}
輸出:
[ = { = "Name": "Diamond Fork", = "Number": 29 = }, = { = "Name": "Sheep Creek", = "Number": 51 = } =]
相關用法
- GO Index.Lookup用法及代碼示例
- GO IndexByte用法及代碼示例
- GO IndexFunc用法及代碼示例
- GO IndexAny用法及代碼示例
- GO IndexRune用法及代碼示例
- GO Index用法及代碼示例
- GO Int.Scan用法及代碼示例
- GO Ints用法及代碼示例
- GO Intn用法及代碼示例
- GO Info用法及代碼示例
- GO Int.SetString用法及代碼示例
- GO Inspect用法及代碼示例
- GO IntsAreSorted用法及代碼示例
- GO Itoa用法及代碼示例
- GO IP.IsPrivate用法及代碼示例
- GO IsDigit用法及代碼示例
- GO IP.Equal用法及代碼示例
- GO IsAbs用法及代碼示例
- GO IP.IsLinkLocalMulticast用法及代碼示例
- GO IsGraphic用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Indent。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。