是否可以将请求值传递给另一个函数?
import "net/http"
func main() {
http.HandleFunc("saySomething", Say)
}
func Say(responseW http.ResponseWriter, request *http.Request) {
name := getName(request) // passing request value to another function
}
func getName(request someType) string {
request.ParseForm()
return request.Form.Get("name")
}
最佳答案
是的,你可以,因为 request
是一个常规变量。
它通过指针传递,因此如果您在 getName
中更改 request
,它也会在 Say
中更改。
package main
import "net/http"
func main() {
http.HandleFunc("saySomething", Say)
}
func Say(responseW http.ResponseWriter, request *http.Request) {
name := getName(request) // passing request value to another function
println(name)
}
func getName(request *http.Request) string {
request.ParseForm()
return request.Form.Get("name")
}
查看 Golang 之旅 https://tour.golang.org/moretypes/1
关于go - 将 "net/http"上的 *Request 传递给 Golang 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44588674/