chore: update go module, readme

This commit is contained in:
zhaoyupeng 2024-08-27 14:39:24 +08:00
parent 5263cba44a
commit 940e86bd8d

View File

@ -5,6 +5,7 @@
##### basic usage ##### basic usage
- get param - get param
```go ```go
func main() { func main() {
app := nf.New() app := nf.New()
@ -19,6 +20,7 @@ func main() {
``` ```
- parse request query - parse request query
```go ```go
func handleQuery(c *nf.Ctx) error { func handleQuery(c *nf.Ctx) error {
type Req struct { type Req struct {
@ -40,6 +42,7 @@ func handleQuery(c *nf.Ctx) error {
``` ```
- parse application/json body - parse application/json body
```go ```go
func handlePost(c *nf.Ctx) error { func handlePost(c *nf.Ctx) error {
type Req struct { type Req struct {
@ -65,3 +68,35 @@ func handlePost(c *nf.Ctx) error {
return c.JSON(nf.Map{"struct": req, "map": reqMap}) return c.JSON(nf.Map{"struct": req, "map": reqMap})
} }
``` ```
- pass local value
```go
type User struct {
Id int
Username string
}
func main() {
app := nf.New()
app.Use(auth())
app.Get("/item/list", list)
}
func auth() nf.HandlerFunc {
return func(c *nf.Ctx) error {
c.Locals("user", &User{Id: 1, Username:"user"})
return c.Next()
}
}
func list(c *nf.Ctx) error {
user, ok := c.Locals("user").(*User)
if !ok {
return c.Status(401).SendString("login required")
}
...
}
```