From version v1.15, the Request object supports binding default values to properties of the input object via struct tag. The name of the default value struct tag is d (it can also be default).

Let’s look at an example for better understanding.

Parameter Object Definition

  1. type GetListReq struct {
  2. g.Meta `path:"/" method:"get"`
  3. Type string `v:"required#Please select content model" dc:"Content model"`
  4. Page int `v:"min:0#Error in pagination number" dc:"Pagination number" d:"1"`
  5. Size int `v:"max:50#Maximum pagination count is 50" dc:"Pagination count, maximum 50" d:"10"`
  6. Sort int `v:"in:0,1,2#Invalid sort type" dc:"Sort type (0: Latest, default. 1: Active, 2: Popular)"`
  7. }
  8. type GetListRes struct {
  9. Items []Item `dc:"Content list"`
  10. }
  11. type Item struct {
  12. Id int64 `dc:"Content ID"`
  13. Title string `dc:"Content title"`
  14. }

This is a parameter object for receiving a request to query the content list. Here, we use the d tag to assign default values to the attributes Page and Size. When these two parameters are not passed, they default to 1 and 10, meaning pagination starts from page 1 and queries 10 items per page.

Parameter Object Usage

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/net/ghttp"
  6. )
  7. type GetListReq struct {
  8. g.Meta `path:"/" method:"get"`
  9. Type string `v:"required#Please select content model" dc:"Content model"`
  10. Page int `v:"min:0#Error in pagination number" dc:"Pagination number" d:"1"`
  11. Size int `v:"max:50#Maximum pagination count is 50" dc:"Pagination count, maximum 50" d:"10"`
  12. Sort int `v:"in:0,1,2#Invalid sort type" dc:"Sort type (0: Latest, default. 1: Active, 2: Popular)"`
  13. }
  14. type GetListRes struct {
  15. Items []Item `dc:"Content list"`
  16. }
  17. type Item struct {
  18. Id int64 `dc:"Content ID"`
  19. Title string `dc:"Content title"`
  20. }
  21. type Controller struct{}
  22. func (Controller) GetList(ctx context.Context, req *GetListReq) (res *GetListRes, err error) {
  23. g.Log().Info(ctx, req)
  24. return
  25. }
  26. func main() {
  27. s := g.Server()
  28. s.Group("/content", func(group *ghttp.RouterGroup) {
  29. group.Middleware(ghttp.MiddlewareHandlerResponse)
  30. group.Bind(&Controller{})
  31. })
  32. s.SetPort(8199)
  33. s.Run()
  34. }

Visit the following addresses and check the server terminal output results:

http://127.0.0.1:8199/content?type=ask

  1. 2023-03-21 21:58:23.058 [INFO] {2883f9c2dc734e170a35c73ea3560b4b} {"Type":"ask","Page":1,"Size":10,"Sort":0}

http://127.0.0.1:8199/content?type=ask&page=

  1. 2023-03-21 21:58:32.555 [INFO] {b86e22f9de734e170b35c73edf07859d} {"Type":"ask","Page":1,"Size":10,"Sort":0}

http://127.0.0.1:8199/content?type=ask&page=2

  1. 2023-03-21 22:01:02.907 [INFO] {a016c8fa01744e170f35c73e99082f53} {"Type":"ask","Page":2,"Size":10,"Sort":0}

As observed, when the client side does not pass or passes an empty page parameter, the default values defined on the server side will be used; however, when the client side passes a specific page parameter, the default values will not take effect.

Considerations

The default value parameter binding is determined by whether the client has not submitted the parameter to identify if the default value is enabled. If the client has submitted the parameter, even if the parameter value is an empty string, it will be treated as the client having passed a specific value, and the default value tag on the server-side data structure will not take effect.