transform.Remarshal
返回按指定格式序列化后的字符串,它由一串序列化数据或一个映射编组而成。
用法
格式必须是 json、toml、yaml 或 xml 之一。如果输入是一串序列化数据,它必须是合法的 JSON、TOML、YAML 或 XML。
示例
下面的示例把数据从一种序列化格式转换为另一种。
TOML 转 JSON
该示例把一串 TOML 转换为 JSON:
{{ $s := `
baseURL = 'https://example.org/'
locale = 'en-US'
title = 'ABC Widgets'
`}}
<pre>{{ transform.Remarshal "json" $s }}</pre>生成的 HTML:
<pre>{
"baseURL": "https://example.org/",
"locale": "en-US",
"title": "ABC Widgets"
}
</pre>在浏览器中的渲染结果:
{
"baseURL": "https://example.org/",
"locale": "en-US",
"title": "ABC Widgets"
}映射转 YAML
该示例把一个映射转换为 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