如何使用 Gorilla Mux 添加查询参数?
到目前为止,这就是我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论