quick/request-struct #122
Replies: 1 comment 1 reply
-
type HelloReq struct {
name string // 姓名
age int // 年龄
}
func main() {
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request) {
var req HelloReq
if err := r.Parse(&req); err != nil {
r.Response.Write(err.Error())
return
}
if req.name == "" {
r.Response.Write("name should not be empty")
return
}
if req.age <= 0 {
r.Response.Write("invalid age value")
return
}
r.Response.Writef("hello %s! your age is %d", req.name, req.age)
})
s.SetPort(8000)
s.Run()
}当 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
quick/request-struct
通过数据结构化解决参数名称硬编码问题,介绍了如何定义请求对象以接收客户端参数,通过GoFrame框架实现参数映射与校验,提高代码可维护性。同时,示例程序展示了避免冗余校验逻辑的方法,探讨了更简洁的解决方案。
https://goframe.org/quick/request-struct
Beta Was this translation helpful? Give feedback.
All reactions