transform.Remarshal
语法
transform.Remarshal FORMAT INPUT
返回值
string
格式必须是 json
、toml
、yaml
或 xml
之一。如果输入是序列化数据的字符串,则必须是有效的 JSON、TOML、YAML 或 XML。
- 示例 1
- 将 TOML 字符串转换为 JSON。
{{ $s := `
baseURL = 'https://example.org/'
languageCode = 'en-US'
title = 'ABC Widgets'
`}}
<pre>{{ transform.Remarshal "json" $s }}</pre>
生成的 HTML
<pre>{
"baseURL": "https://example.org/",
"languageCode": "en-US",
"title": "ABC Widgets"
}
</pre>
在浏览器中渲染
{
"baseURL": "https://example.org/",
"languageCode": "en-US",
"title": "ABC Widgets"
}
- 示例 2
- 将映射转换为 YAML。
{{ $m := dict
"a" "Hugo rocks!"
"b" (dict "question" "What is 6x7?" "answer" 42)
"c" (slice "foo" "bar")
}}
<pre>{{ transform.Remarshal "yaml" $m }}</pre>
生成的 HTML
<pre>a: Hugo rocks!
b:
answer: 42
question: What is 6x7?
c:
- foo
- bar
</pre>
在浏览器中渲染
a: Hugo rocks!
b:
answer: 42
question: What is 6x7?
c:
- foo
- bar