英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论