#sample.yml
single: |
a single quote'
double: |
a double quote"
// main.go
package main
import (
"html/template"
"io/ioutil"
"log"
"os"
"gopkg.in/yaml.v2"
)
const Template = `{{ .Single}}
---
{{ .Double}}`
type Sample struct {
Single string `yaml:"single"`
Double string `yaml:"double"`
}
func main() {
file, err := ioutil.ReadFile("sample.yml")
if err != nil {
log.Fatal(err)
}
s := new(Sample)
err = yaml.Unmarshal(file, s)
if err != nil {
log.Fatal(err)
}
t, err := template.New("s").Parse(Template)
if err != nil {
log.Fatal(err)
}
err = t.Execute(os.Stdout, s)
if err != nil {
log.Fatal()
}
}
打印出来的是 '
这样的字符.
如果想要保留原始的 ' " , 要如何做呀?
$ go run ./main.go
a single quote'
---
a double quote"
1
vZexc0m 2020-10-12 09:41:06 +08:00
|
2
heimeil 2020-10-12 09:54:23 +08:00 2
type Sample struct {
Single template.HTML `yaml:"single"` Double template.HTML `yaml:"double"` } |
4
zunceng 2020-10-12 13:59:03 +08:00 1
你这代码 和 yaml 没半毛钱关系 是 template 干的
import “html/template” => import “text/template” |