Skip to content

Commit 35dcc94

Browse files
Add docs from gofiber/fiber@3fc1b29
1 parent 8d954b8 commit 35dcc94

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

docs/core/api/bind.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,40 @@ app.Post("/", func(c fiber.Ctx) error {
573573
}
574574
})
575575
```
576+
577+
## Default Fields
578+
579+
You can set default values for fields in the struct by using the `default` struct tag. Supported types:
580+
581+
- bool
582+
- float variants (float32, float64)
583+
- int variants (int, int8, int16, int32, int64)
584+
- uint variants (uint, uint8, uint16, uint32, uint64)
585+
- string
586+
- a slice of the above types. As shown in the example above, **| should be used to separate between slice items**.
587+
- a pointer to one of the above types **(pointer to slice and slice of pointers are not supported)**.
588+
589+
```go title="Example"
590+
type Person struct {
591+
Name string `query:"name,default:john"`
592+
Pass string `query:"pass"`
593+
Products []string `query:"products,default:shoe|hat"`
594+
}
595+
596+
app.Get("/", func(c fiber.Ctx) error {
597+
p := new(Person)
598+
599+
if err := c.Bind().Query(p); err != nil {
600+
return err
601+
}
602+
603+
log.Println(p.Name) // john
604+
log.Println(p.Pass) // doe
605+
log.Println(p.Products) // ["shoe,hat"]
606+
607+
// ...
608+
})
609+
// Run tests with the following curl command
610+
611+
// curl "http://localhost:3000/?pass=doe"
612+
```

0 commit comments

Comments
 (0)