2

I am trying to do the following:

/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

...

I tried the following in GIN:

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

Also tried:

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

Unfortunately, it does not work. It does not recognize these routes.

Is there a way to make it work and how to make it with a GIN or with "basic" GO?

Filip
  • 401
  • 8
  • does it have to be done with query parameters? I know you could do it with different URLs (i.e. `/car/color/{colour}`) – blurfus Dec 30 '22 at 15:15
  • @blurfus Yes, I need it with query parameters. – Filip Dec 30 '22 at 15:18
  • as far as I know, there is no way to do it based on query parameters – blurfus Dec 30 '22 at 16:10
  • 1
    Write a handler for /car that dispatches to other handlers using the query parameters. – Charlie Tumahai Dec 30 '22 at 17:17
  • @CeriseLimón If I understand correctly, you are suggesting that I write one function handler for /car and then inside that handler create if-else-if statements that, depending on the query parameters, will call the appropriate functions? – Filip Dec 30 '22 at 17:59
  • 1
    @Filip Yes. A switch or table driven approach may also be appropriate. – Charlie Tumahai Dec 30 '22 at 18:05

1 Answers1

0

Is there a way to make it work and how to make it with a GIN

No.

or with "basic" GO?

No.

Volker
  • 40,468
  • 7
  • 81
  • 87