transform.Remarshal

格式必須是 jsontomlyamlxml 之一。如果輸入是一個序列化資料字串,則必須是有效的 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>{
   &#34;baseURL&#34;: &#34;https://example.org/&#34;,
   &#34;languageCode&#34;: &#34;en-US&#34;,
   &#34;title&#34;: &#34;ABC Widgets&#34;
}
</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