如何使用 Gorilla Mux 添加查询参数?

发布于 2025-01-12 21:42:30 字数 1663 浏览 0 评论 0原文

到目前为止,这就是我使用 Gorilla Mux 构建路线的方式。现在我需要将查询参数添加到我的路线中。这就是我此时所做的:

type Route struct {
    Name       string
    Method     string
    Pattern    string
    HandleFunc http.HandlerFunc
}

func NewRouter(sqlDb *data.Data, redisClient *redis.Client) Router {
    return &router{
        sqlDb:       sqlDb,
        redisClient: redisClient,
    }
}

func (r *router) MapRoutes() {
    r.buildUserRoutes()
}

func routesCreation(routes Routes, r *router) {

    for _, route := range routes {
        r.Router.Methods(route.Method).
            Path(route.Pattern).
            Name(route.Name).
            Handler(route.HandleFunc)
    }
}

var userRoutes = Routes{
    Route{"ping", "GET", "/ping", handler.Ping},
}

这就是我尝试过的,添加一段字符串作为 Route 结构上的字段,但它不起作用:

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

func NewRouter(sqlDb *data.Data, redisClient *redis.Client) Router {
    return &router{
        sqlDb:       sqlDb,
        redisClient: redisClient,
    }
}

func (r *router) MapRoutes() {
    r.buildUserRoutes()
}

func (r *router) buildUserRoutes() {

    p := []string{"a", "b"}

    var userRoutes = Routes{
        Route{"ping", "GET", "/ping", handler.Ping, p},
}

func routesCreation(routes Routes, r *router) {

    for _, route := range routes {
        r.Router.Methods(route.Method).
            Path(route.Pattern).
            Name(route.Name).
            Handler(route.HandleFunc).
            Queries(route.Queries)
    }
}

是否可以使用此配置添加查询参数路线创作?

谢谢!

This is how i build my routes at the time so far using Gorilla Mux. Now i need to add query params to my routes. This is how im doing at this moment:

type Route struct {
    Name       string
    Method     string
    Pattern    string
    HandleFunc http.HandlerFunc
}

func NewRouter(sqlDb *data.Data, redisClient *redis.Client) Router {
    return &router{
        sqlDb:       sqlDb,
        redisClient: redisClient,
    }
}

func (r *router) MapRoutes() {
    r.buildUserRoutes()
}

func routesCreation(routes Routes, r *router) {

    for _, route := range routes {
        r.Router.Methods(route.Method).
            Path(route.Pattern).
            Name(route.Name).
            Handler(route.HandleFunc)
    }
}

var userRoutes = Routes{
    Route{"ping", "GET", "/ping", handler.Ping},
}

This is what i tried, adding a slice of string as a field on Route struct but it did not worked:

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

func NewRouter(sqlDb *data.Data, redisClient *redis.Client) Router {
    return &router{
        sqlDb:       sqlDb,
        redisClient: redisClient,
    }
}

func (r *router) MapRoutes() {
    r.buildUserRoutes()
}

func (r *router) buildUserRoutes() {

    p := []string{"a", "b"}

    var userRoutes = Routes{
        Route{"ping", "GET", "/ping", handler.Ping, p},
}

func routesCreation(routes Routes, r *router) {

    for _, route := range routes {
        r.Router.Methods(route.Method).
            Path(route.Pattern).
            Name(route.Name).
            Handler(route.HandleFunc).
            Queries(route.Queries)
    }
}

Is it possible to add query params with this config for routes creations?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文