Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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和gorilla mux的动态路由查询_Go - Fatal编程技术网

使用GO和gorilla mux的动态路由查询

使用GO和gorilla mux的动态路由查询,go,Go,我正在使用gorilla mux设置动态路由,下面是routes.go代码 type Route struct { Name string Method string Pattern string Queries []string HandlerFunc http.HandlerFunc } type Routes []Route var vers = os.Getenv("API_VERSION") var b

我正在使用gorilla mux设置动态路由,下面是routes.go代码

type Route struct {
    Name        string
    Method      string
    Pattern     string
    Queries     []string
    HandlerFunc http.HandlerFunc
}

type Routes []Route

var vers = os.Getenv("API_VERSION")
var baseURL = "/api/" + vers + "/"

var authRoutes = Routes{
    Route{
        "GetAllUsers",
        "GET",
        baseURL + "users",
        []string{"maxperpage", "{maxperpage}", "setpage", "{setpage}"},
        ssoUserController.GetAllUsers,
    },
}

我想用切片创建动态查询,如下所示:

func Handlers() *mux.Router {

//.....
//some code
//.....

    s := r.PathPrefix("/auth").Subrouter()
    s.Use(utils.JwtVerify)
    for _, authRoute := range authRoutes {
        var handler http.Handler
        var tamp = []string{}

        handler = authRoute.HandlerFunc
        handler = Logger(handler, authRoute.Name)
        for _, q := range authRoute.Queries {

            var addQuot = strconv.Quote(q)
            tamp = append(tamp, addQuot)

        }
        queries := strings.Join(tamp, ", ") //this code create string "maxperpage", "{maxperpage:[0-99]+}", "setpage", "{setpage:[0-99]+}"

        // fmt.Println(queries)
        s.
            Methods(authRoute.Method).
            Path(authRoute.Pattern).
            Queries(queries). // produce error panic: runtime error: invalid memory address or nil pointer dereference
            Name(authRoute.Name).
            Handler(handler)

    }

    return r
}
上面的代码是router.go文件,它将路由routes.go文件中的所有路由,当我将查询切片时,它将产生错误

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x20 pc=0x77a750] 
请帮助使用gorilla mux放置动态url查询

您使用的查询()错误。它获取成对的字符串,而不是单个字符串。使用查询(authRoute.querys…)应该可以工作