summaryrefslogtreecommitdiff
path: root/playground/main.go
blob: 38b5f0d8d01790163d9c51f75c45b8844624f22f (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
35
36
37
38
39
40
41
42
43
44
45
package main

import (
	"encoding/json"
	"fmt"
	"html/template"
	"log"
	"os"
)

type Testy struct {
	Flag *bool     `json:"flag"`
	Cake *[]string `json:"cake"`
}

const TEXT = `{
	"cake": [
		"one",
		"two",
		"three"
	]
}`

const TEMPLATE = `Flag is: {{if .Flag}}{{.Flag}}{{else}}[none]{{end}}
Cake is: {{if .Cake}}{{range .Cake}}{{.}} {{end}}{{else}}[none]{{end}}
`

func main() {
	var t Testy
	json.Unmarshal([]byte(TEXT), &t)

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

	tmpl.Execute(os.Stdout, t)

	b, err := json.Marshal(t)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(b))
}