text templates

text templates

原文:https://gobyexample.com/text-templates

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main

import (
	"os"
	"text/template"
)

func main() {
	t1 := template.New("t1")
	t1, err := t1.Parse("Value is {{.}}\n")
	if err != nil {
		panic(err)
	}

	t1 = template.Must(t1.Parse("Value: {{.}}\n"))

	t1.Execute(os.Stdout, "some text") // Value: some text
	t1.Execute(os.Stdout, 5)           // Value: 5
	t1.Execute(os.Stdout, []string{
		"Go",
		"Rust",
		"C++",
		"C#",
	}) // Value: [Go Rust C++ C#]

	Create := func(name, t string) *template.Template {
		return template.Must(template.New(name).Parse(t))
	}

	t2 := Create("t2", "Name: {{.Name}}\n")

	t2.Execute(os.Stdout, struct {
		Name string
	}{"Jane Doe"}) // Name: Jane Doe

	t2.Execute(os.Stdout, map[string]string{
		"Name": "Mickey Mouse",
	}) // Name: Mickey Mouse

	t3 := Create("t3",
		"{{if . -}} yes {{else -}} no {{end}}\n")

	t3.Execute(os.Stdout, "not empty") // yes
	t3.Execute(os.Stdout, "")          // no

	t4 := Create("t4",
		"Range: {{range .}}{{.}} {{end}}\n")

	t4.Execute(os.Stdout,
		[]string{
			"Go",
			"Rust",
			"C++",
			"C#",
		}) // Range: Go Rust C++ C#
}
最后修改 October 10, 2024: 更新 (a4b8f85)