File: main.go

package info (click to toggle)
golang-github-yosssi-ace 0.0.4%2Bgit20160102.51.71afeb7-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 468 kB
  • ctags: 249
  • sloc: makefile: 3; sh: 1
file content (38 lines) | stat: -rw-r--r-- 748 bytes parent folder | download | duplicates (3)
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 (
	"net/http"

	"github.com/yosssi/ace"
)

// Pet represents a pet.
type Pet struct {
	Species string
	Name    string
	Age     int
}

func handler(w http.ResponseWriter, r *http.Request) {
	tpl, err := ace.Load("example", "", nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	data := map[string]interface{}{
		"Pets": []Pet{
			Pet{Species: "Dog", Name: "Taro", Age: 5},
			Pet{Species: "Cat", Name: "Hanako", Age: 10},
			Pet{Species: "Rabbit", Name: "Jiro", Age: 1},
		},
	}
	if err := tpl.Execute(w, data); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}