-
Notifications
You must be signed in to change notification settings - Fork 0
/
else.go
48 lines (40 loc) · 1007 Bytes
/
else.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
package txttmpl
import (
"bytes"
"text/template"
"github.com/devlights/gomy/output"
)
// Else -- text/template の テンプレート仕様 におけるelseのサンプルです.
//
// # REFERENCES
// - https://pkg.go.dev/text/template@latest
// - https://ema-hiro.hatenablog.com/entry/20170729/1501320887
// - https://stackoverflow.com/questions/33282061/go-templates-with-eq-and-index
// - https://stackoverflow.com/questions/31101729/compare-strings-in-templates
func Else() error {
const (
t1 = `{{if eq .Message "hello" -}} world {{- else if eq .Message "world" -}} hello {{- else -}} helloworld {{- end}}`
)
type (
D struct {
Message string
}
)
for _, t := range []string{t1} {
var (
tmpl *template.Template
buf bytes.Buffer
err error
)
tmpl, err = template.New("ElseIf").Parse(t)
if err != nil {
return err
}
err = tmpl.Execute(&buf, D{"golang"})
if err != nil {
return err
}
output.Stdoutl("[tmpl]", buf.String())
}
return nil
}