Taking GET parameters as func arguments or using c.Params.Get() in Golang Revel

huangapple go评论100阅读模式
英文:

Taking GET parameters as func arguments or using c.Params.Get() in Golang Revel

问题

Golang Revel web框架中,将函数参数设置为参数(用于GET和POST请求)与在函数内部获取HTTP参数有什么区别?

func (c Machine) TestConnection(addr string, port int, username, password string) revel.Result

addr := c.Params.Get("addr")
port, _ := strconv.Atoi(c.Params.Get("port"))
username := c.Params.Get("username")
password := c.Params.Get("password")

另外,如果我使用函数参数的方法(第一种方法),我还可以使用c.Validation.Required("addr").Ok来验证HTTP参数吗?

英文:

In the Golang Revel web framework, what's the difference between setting function arguments as parameters (for both GET and POST)

func (c Machine) TestConnection(addr string, port int, username, password string) revel.Result

versus retrieving HTTP parameters from within the function

addr := c.Params.Get("addr")
port, _ := strconv.Atoi(c.Params.Get("port"))
username := c.Params.Get("username")
password := c.Params.Get("password")

Also, if I use the function arguments method (the first method), can I still validate the HTTP parameters with c.Validation.Required("addr").Ok?

答案1

得分: 1

你可以使用任何你喜欢的方式。然而,将它们定义为方法参数可以让框架负责将请求中的字符串解析为所需的类型,这样更加方便。

英文:

You can use whichever you prefer. However, defining them as method parameters lets the framework take care of parsing the string from the request to the type that you need. So it's offered as convenience.

huangapple
  • 本文由 发表于 2017年9月7日 16:46:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/46091854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定