chore: update go module, readme

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

View File

@ -5,8 +5,9 @@
##### basic usage
- get param
```go
func main() {
```go
func main() {
app := nf.New()
app.Get("/hello/:name", func(c *nf.Ctx) error {
@ -15,12 +16,13 @@ func main() {
})
log.Fatal(app.Run("0.0.0.0:80"))
}
```
}
```
- parse request query
```go
func handleQuery(c *nf.Ctx) error {
```go
func handleQuery(c *nf.Ctx) error {
type Req struct {
Name string `query:"name"`
Addr []string `query:"addr"`
@ -36,12 +38,13 @@ func handleQuery(c *nf.Ctx) error {
}
return c.JSON(nf.Map{"query": req})
}
```
}
```
- parse application/json body
```go
func handlePost(c *nf.Ctx) error {
```go
func handlePost(c *nf.Ctx) error {
type Req struct {
Name string `json:"name"`
Addr []string `json:"addr"`
@ -63,5 +66,37 @@ func handlePost(c *nf.Ctx) error {
}
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")
}
...
}
```