summaryrefslogtreecommitdiff
path: root/optional/template_test.go
blob: ab4ce768d02a834f8ee1e190a54229b1b6fda0ed (plain)
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
package optional_test

import (
	"html/template"
	"log"
	"os"

	"xengineering.eu/optional-go/optional"
)

func ExampleTemplate() {
	data := struct {
		Flag optional.Optional[bool]
	}{
		Flag: optional.Optional[bool]{
			Value:  false,
			Exists: false,
		},
	}

	tmpl := `Flag is: {{if .Flag.Exists}}{{.Flag.Value}}{{else}}[none]{{end}}`

	t, err := template.New("optional").Parse(tmpl)
	if err != nil {
		log.Fatal(err)
	}

	err = t.Execute(os.Stdout, data)
	if err != nil {
		log.Fatal(err)
	}

	// Output: Flag is: [none]
}