首页 > 文章列表 > 如何在 Golang 或 GIN 中将两个 GET 查询参数映射到不同的路由(处理函数)上?

如何在 Golang 或 GIN 中将两个 GET 查询参数映射到不同的路由(处理函数)上?

325 2024-02-06
问题内容

我正在尝试执行以下操作:

/car?color= -> first route (endpoint) and first handler function
/car?brand= -> second route (endpoint) and second handler function
/car?type= -> third route (endpoint) and third handler function
...

我在 gin 中尝试了以下操作:

server.get("/car?color=", carcolorhandlerfunction)
server.get("/car?brand=", carbrandhandlerfunction)
server.get("/car?type=", cartypehandlerfunction)

还尝试过:

server.GET("/car?color=:carColor", carColorHandlerFunction)
server.GET("/car?brand=:carBrand", carBrandHandlerFunction)
server.GET("/car?type=:carType", carTypeHandlerFunction)

不幸的是,它不起作用。它无法识别这些路由。

有没有办法让它发挥作用,以及如何用 gin 或“基本”go 来实现它?


正确答案


有没有办法让它发挥作用以及如何用 GIN 实现它

没有。

还是使用“基本”GO?

没有。