Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回带有Go参数的函数的函数?_Go - Fatal编程技术网

返回带有Go参数的函数的函数?

返回带有Go参数的函数的函数?,go,Go,我已经在JS中完成了这项工作,不确定在Go中是否可行,但我希望从具有参数的函数返回一个函数作为返回类型 实例 调用的方式如下所示: thing:=StoreTask(myDb)然后我可以通过HTTP库将其作为处理程序调用,以供以后使用 可以使用以下语法执行此操作: func StoreTask(db *sql.DB) func(http.ResponseWriter, *http.Request) { return func(wr http.ResponseWriter, r *http.R

我已经在JS中完成了这项工作,不确定在Go中是否可行,但我希望从具有参数的函数返回一个函数作为返回类型

实例 调用的方式如下所示:
thing:=StoreTask(myDb)
然后我可以通过HTTP库将其作为处理程序调用,以供以后使用

可以使用以下语法执行此操作:

func StoreTask(db *sql.DB) func(http.ResponseWriter, *http.Request) {
  return func(wr http.ResponseWriter, r *http.Request) {
    fmt.Printf("This is the store task GET request %s", strings.Split(r.URL.Path, "/")[1])
    for _, val := range r.URL.Query() {
        fmt.Printf("Current val is: %s\n", val[0])
    }
  }
}

这里,
StoreTask
返回一个获取HTTP处理程序的函数,该处理程序还可以使用传递到
StoreTask

db
参数。唯一的问题是
func(rw,r){
不是一个正确的函数定义,因为没有用于参数的类型。太棒了,谢谢!我想我的主要问题是我在outter函数的返回值类型中添加了变量名,但没有意识到我不能这样做。不,主要/唯一的问题是“内部”函数缺少类型。
func StoreTask(db *sql.DB) func(http.ResponseWriter, *http.Request) {
  return func(wr http.ResponseWriter, r *http.Request) {
    fmt.Printf("This is the store task GET request %s", strings.Split(r.URL.Path, "/")[1])
    for _, val := range r.URL.Query() {
        fmt.Printf("Current val is: %s\n", val[0])
    }
  }
}