Gorilla Mux 相同端点多个查询参数

Gorilla Mux same endpoint multiple query params

所以我知道 Gorilla Mux 不支持可选的查询参数,人们建议使用查询参数创建不同的路由,这使得它更可靠。但就我而言,没有按预期解析路由

如果我调用 /service/{locale}?param1=1,2&param2=3,4 然后它调用 a.listFirst 而它应该调用 a.listSecond 和其他组合的相同问题。但是:

  1. 如果我只保留一条路线,那么这条路线会按预期工作。所以我假设路线本身没问题,但当它们在一起时会出现一些解决问题?
  2. 如果我删除第一条路线(没有查询参数的路线)并交换其余路线的顺序,那么它们都可以正常工作。这意味着顺序也很重要?

我知道我可以使用 request.URL.Query() 来获取查询参数,但我很想知道为什么这种将查询参数定义为路由的方式无法按预期工作?

a.Router.
    Methods(http.MethodGet).
    Path("/service/{locale}").
    Handler(negroni.New(
        a.contentTypeJSON,
        negroni.HandlerFunc(a.listAll),
    ))

a.Router.
    Methods(http.MethodGet).
    Path("/service/{locale}").
    Queries("param1", "{param1:[0-9,]+}").
    Handler(negroni.New(
        a.contentTypeJSON,
        negroni.HandlerFunc(a.listFirst),
    ))

a.Router.
    Methods(http.MethodGet).
    Path("/service/{locale}").
    Queries("param2", "{param2:[0-9,]+}", "param1", "{param1:[0-9,]+}").
    Handler(negroni.New(
        a.contentTypeJSON,
        negroni.HandlerFunc(a.listSecond),
    ))

我已经看过这些但他们没有回答问题link1

更新

简答:订单很重要

提示:别傻了,使用正确的处理函数

您必须更改顺序,因为顺序很重要。第一场比赛被选中

a.Router.
    Methods(http.MethodGet).
    Path("/service/{locale}").
    Queries("param2", "{param2:[0-9,]+}", "param1", "{param1:[0-9,]+}").
    Handler(negroni.New(
        a.contentTypeJSON,
        negroni.HandlerFunc(a.listSecond),
    ))

a.Router.
    Methods(http.MethodGet).
    Path("/service/{locale}").
    Queries("param1", "{param1:[0-9,]+}").
    Handler(negroni.New(
        a.contentTypeJSON,
        negroni.HandlerFunc(a.listFirst),
    ))

a.Router.
    Methods(http.MethodGet).
    Path("/service/{locale}").
    Handler(negroni.New(
        a.contentTypeJSON,
        negroni.HandlerFunc(a.listAll),
    ))

我建议使用 grpc-go 、 grpc-gateway 并尝试使用它,您不需要以这种方式定义额外的查询参数。您消息中定义的所有内容都可以作为查询参数传递