-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.go
53 lines (44 loc) · 1.1 KB
/
new.go
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
package txttmpl
import (
"bytes"
"text/template"
"github.com/devlights/gomy/output"
)
// New -- text/template の Newメソッドのサンプルです.
//
// # REFERENCES
// - https://pkg.go.dev/text/template@latest
func New() error {
//
// テンプレートの使い方
//
// 1. template.New().Parse()
// 2. *text.Template.Execute()
//
// Goのテンプレートでは {{ }} で囲っている部分が解釈される仕様となっている.
// {{ . }} とすれば、*template.Template.Execute(io.Writer, interface{}) に指定した
// データ (interface{}) そのものを指定していることになる。
//
// なので、 {{ .Message }} とかすると、指定したデータの Message フィールドの値を
// 表示することが出来る
//
const (
t = `hello {{.}}`
)
var (
tmpl *template.Template
buf bytes.Buffer
err error
)
tmpl, err = template.New("New").Parse(t)
if err != nil {
return err
}
err = tmpl.Execute(&buf, "world")
if err != nil {
return err
}
output.Stdoutl("[template ]", t)
output.Stdoutl("[tmpl-exec]", buf.String())
return nil
}