-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (52 loc) · 1.21 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"html/template"
"net/http"
"time"
"github.com/dmaizel/my-gin/gin"
)
type student struct {
Name string
Age int8
}
func FormatAsDate(t time.Time) string {
year, month, day := t.Date()
return fmt.Sprintf("%d-%02d-%02d", year, month, day)
}
func main() {
r := gin.New()
r.Use(gin.Logger())
r.SetFuncMap(template.FuncMap{
"FormatAsDate": FormatAsDate,
})
r.LoadHTMLGlob("templates/*")
r.Static("/assets", "./static")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "css.tmpl", nil)
})
stu1 := &student{Name: "Daniel", Age: 20}
stu2 := &student{Name: "Jack", Age: 22}
r.GET("/students", func(c *gin.Context) {
c.HTML(http.StatusOK, "arr.tmpl", gin.H{
"title": "gin",
"stuArr": [2]*student{stu1, stu2},
})
})
r.GET("/date", func(c *gin.Context) {
c.HTML(http.StatusOK, "custom_func.tmpl", gin.H{
"title": "gin",
"now": time.Date(2020, 9, 18, 0, 0, 0, 0, time.UTC),
})
})
r2 := gin.Default()
r2.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello World\n")
})
// index out of range for testing Recovery()
r2.GET("/panic", func(c *gin.Context) {
names := []string{"Daniel"}
c.String(http.StatusOK, names[100])
})
r2.Run(":9998")
}