gin学习之路

 1 package main
 2 
 3 import "github.com/gin-gonic/gin"
 4 
 5 func main() {
 6     r := gin.Default()
 7     r.GET("/ping", func(c *gin.Context) {
 8         c.JSON(200, gin.H{
 9             "message": "pong",
10         })
11     })
12     r.Run()
13 }

 

 

AsciiJSON

 使用 AsciiJSON 生成具有转义的非 ASCII 字符的 ASCII-only JSON。

 1 package main
 2 
 3 import (
 4     "net/http"
 5 
 6     "github.com/gin-gonic/gin"
 7 )
 8 
 9 func main() {
10     r := gin.Default()
11 
12     r.GET("/someJSON", func(c *gin.Context) {
13         data := map[string]interface{}{
14             "lang": "Golang",
15             "tag":  "<br>",
16         }
17         c.AsciiJSON(http.StatusOK, data)
18     })
19 
20     r.Run(":8080")
21 }

 

 

 

 HTML渲染

package main

import (
    "fmt"
    "html/template"
    "net/http"
    "time"

    "github.com/gin-gonic/gin"
)

func formatAsDate(t time.Time) string {
    year, month, day := t.Date()
    return fmt.Sprintf("%d/%02d/%02d", year, month, day)
}

func main() {
    router := gin.Default()
    router.Delims("{[{", "}]}")
    router.SetFuncMap(template.FuncMap{
        "formatAsDate": formatAsDate,
    })
    router.LoadHTMLFiles("raw.tmpl")

    router.GET("/raw", func(c *gin.Context) {
        c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{
            "now": time.Date(2019, 11, 26, 0, 0, 0, 0, time.UTC),
        })
    })

    router.Run(":8080")
}

 

 

 

 HTTP2

 1 package main
 2 
 3 import (
 4     "html/template"
 5     "log"
 6     "net/http"
 7     "os"
 8 
 9     "github.com/gin-gonic/gin"
10 )
11 
12 var html = template.Must(template.New("https").Parse(`
13 <html>
14 <head>
15   <title>Https Test</title>
16 </head>
17 <body>
18   <h1 style="color:red;">Welcome, Ginner!</h1>
19 </body>
20 </html>
21 `))
22 
23 func main() {
24     logger := log.New(os.Stderr, "", 0)
25     logger.Println("[WARNING] DON'T USE THE EMBED CERTS FROM THIS EXAMPLE IN PRODUCTION ENVIRONMENT, GENERATE YOUR OWN!")
26 
27     r := gin.Default()
28     r.SetHTMLTemplate(html)
29 
30     r.GET("/welcome", func(c *gin.Context) {
31         c.HTML(http.StatusOK, "https", gin.H{
32             "status": "success",
33         })
34     })
35 
36     // Listen and Server in https://127.0.0.1:8080
37     r.RunTLS(":8080", "./testdata/server.pem", "./testdata/server.key")
38 }

 

 

 

 

只绑定 url 查询字符串

 1 package main
 2 
 3 import (
 4     "log"
 5 
 6     "github.com/gin-gonic/gin"
 7 )
 8 
 9 type Person struct {
10     Name    string `form:"name"`
11     Address string `form:"address"`
12 }
13 
14 func main() {
15     route := gin.Default()
16     route.Any("/testing", startPage)
17     route.Run(":8085")
18 }
19 
20 func startPage(c *gin.Context) {
21     var person Person
22     if c.ShouldBindQuery(&person) == nil {
23         log.Println("==Only Bind by Query String==")
24         log.Println(person.Name)
25         log.Println(person.Address)
26     }
27     c.String(200, "success")
28 }

 

posted @ 2019-11-26 15:49  尘归风  阅读(251)  评论(0)    收藏  举报