plimble/ace 是个后起的Golang web 框架,大家还不熟知。但性能很好,Handler 表现方式也灵活。

plimble/ace

简单示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main

import (
    "github.com/plimble/ace"
)

func main() {
    a := ace.Default()
    //a := ace.New()
    a.GET("/:name", func(c *ace.C) {
        name := c.Param("name")
        c.JSON(200, map[string]string{"hello": name})
    })
    a.Run(":8080")
}

路由形式

1
2
3
4
5
6
7
a.DELETE("/", HandlerFunc)
a.HEAD("/", HandlerFunc)
a.OPTIONS("/", HandlerFunc)
a.PATCH("/", HandlerFunc)
a.PUT("/", HandlerFunc)
a.POST("/", HandlerFunc)
a.GET("/", HandlerFunc)

路由示例

1
2
3
4
5
6
7
8
9
10
11
a := ace.New()

a.GET("/", func(c *ace.C){
    c.String(200, "Hello world")
})

a.POST("/form/:id/:name", func(c *ace.C){
    id := c.Param("id")
    name := c.Param("name")
    age := c.Request.PostFormValue("age")
})

不同形式的输出

JSON

1
2
3
4
5
6
data := struct{
    Name string `json:"name"`
}{
    Name: "John Doe",
}
c.JSON(200, data)

String

1
c.String(200, "Hello Ace")

Download

1
2
//application/octet-stream
c.Download(200, []byte("Hello Ace"))

HTML

1
c.HTML("index.html")

Redirect

1
c.Redirect("/home")

本文网址: https://golangnote.com/topic/18.html 转摘请注明来源