@kjk1/

gobook template len

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.11942326552779448","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"cGFja2FnZSBtYWluCgppbXBvcnQgKAoJImxvZyIKCSJvcyIKCSJ0ZXh0L3RlbXBsYXRlIgopCgovLyA6c2hvdyBzdGFydApjb25zdCB0bXBsU3RyID0gYGxlbihuaWwpICAgICAgIDoge3sgbGVuIC5TbGljZU5pbCB9fQpsZW4oZW1wdHlTbGljZSk6IHt7IGxlbiAuU2xpY2VFbXB0eSB9fQpsZW4oc2xpY2UpICAgICA6IHt7IGxlbiAuU2xpY2UgfX0KbGVuKG1hcCkgICAgICAgOiB7eyBsZW4gLk1hcCB9fQpgCgovLyA6c2hvdyBlbmQKCmZ1bmMgbWFpbigpIHsKCS8vIDpzaG93IHN0YXJ0Cgl0IDo9IHRlbXBsYXRlLk11c3QodGVtcGxhdGUuTmV3KCJsZW4iKS5QYXJzZSh0bXBsU3RyKSkKCglkYXRhIDo9IHN0cnVjdCB7CgkJU2xpY2VOaWwgICBbXWludAoJCVNsaWNlRW1wdHkgW11zdHJpbmcKCQlTbGljZSAgICAgIFtdYm9vbAoJCU1hcCAgICAgICAgbWFwW2ludF1ib29sCgl9ewoJCVNsaWNlTmlsOiAgIG5pbCwKCQlTbGljZUVtcHR5OiBbXXN0cmluZ3t9LAoJCVNsaWNlOiAgICAgIFtdYm9vbHt0cnVlLCB0cnVlLCBmYWxzZX0sCgkJTWFwOiAgICAgICAgbWFwW2ludF1ib29sezU6IHRydWUsIDM6IGZhbHNlfSwKCX0KCWVyciA6PSB0LkV4ZWN1dGUob3MuU3Rkb3V0LCBkYXRhKQoJaWYgZXJyICE9IG5pbCB7CgkJbG9nLkZhdGFsZigidC5FeGVjdXRlKCkgZmFpbGVkIHdpdGggJyVzJ1xuIiwgZXJyKQoJfQoJLy8gOnNob3cgZW5kCn0="},"asBuffer":null},"loaded":true}}
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
package main

import (
	"log"
	"os"
	"text/template"
)

// :show start
const tmplStr = `len(nil)       : {{ len .SliceNil }}
len(emptySlice): {{ len .SliceEmpty }}
len(slice)     : {{ len .Slice }}
len(map)       : {{ len .Map }}
`

// :show end

func main() {
	// :show start
	t := template.Must(template.New("len").Parse(tmplStr))

	data := struct {
		SliceNil   []int
		SliceEmpty []string
		Slice      []bool
		Map        map[int]bool
	}{
		SliceNil:   nil,
		SliceEmpty: []string{},
		Slice:      []bool{true, true, false},
		Map:        map[int]bool{5: true, 3: false},
	}
	err := t.Execute(os.Stdout, data)
	if err != nil {
		log.Fatalf("t.Execute() failed with '%s'\n", err)
	}
	// :show end
}