github.com/gogf/gf@v1.16.9/.example/net/ghttp/server/request/struct/parse2.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/gogf/gf/frame/g"
     5  	"github.com/gogf/gf/net/ghttp"
     6  )
     7  
     8  type RegisterReq struct {
     9  	Name  string
    10  	Pass  string `p:"password1"`
    11  	Pass2 string `p:"password2"`
    12  }
    13  
    14  type RegisterRes struct {
    15  	Code  int         `json:"code"`
    16  	Error string      `json:"error"`
    17  	Data  interface{} `json:"data"`
    18  }
    19  
    20  func main() {
    21  	s := g.Server()
    22  	s.BindHandler("/register", func(r *ghttp.Request) {
    23  		var req *RegisterReq
    24  		if err := r.Parse(&req); err != nil {
    25  			r.Response.WriteJsonExit(RegisterRes{
    26  				Code:  1,
    27  				Error: err.Error(),
    28  			})
    29  		}
    30  		// ...
    31  		r.Response.WriteJsonExit(RegisterRes{
    32  			Data: req,
    33  		})
    34  	})
    35  	s.SetPort(8199)
    36  	s.Run()
    37  }