Gin: router two parameters

Created on 11 Aug 2015  路  3Comments  路  Source: gin-gonic/gin

HI
Is it possible to do a route like this

//route 1
r.Get("/product/:id/)

//route 2
r.Get("/product/:id/shop/:shopid")
question

All 3 comments

Anyone :( ?

Yes.

charrington@localhost ~/go/tmp $ cat test.go
package main

import "github.com/gin-gonic/gin"
import "log"

func main() {
  router := gin.Default()
  router.GET("/a/:a", func(c *gin.Context) {
    a1 := c.Param("a")
    log.Printf("/a/:a route invoked. a=%q\n", a1);
  });
  router.GET("/a/:a/b/:b", func(c *gin.Context) {
    a2 := c.Param("a")
    b2 := c.Param("b")
    log.Printf("/a/:a/b/:b route invoked. a=%q, b=%q\n", a2, b2);
  });
  router.Run(":8080")
}
charrington@localhost ~/go/tmp $ go run test.go
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET   /a/:a                     --> main.func路001 (3 handlers)
[GIN-debug] GET   /a/:a/b/:b                --> main.func路002 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080
/a/:a route invoked. a="Apple"
[GIN] 2015/08/13 - 16:05:47 | 200 |    1.194896ms | 10.10.10.1:60526 |   GET     /a/Apple
/a/:a/b/:b route invoked. a="Apple", b="Banana"
[GIN] 2015/08/13 - 16:05:50 | 200 |     464.636碌s | 10.10.10.1:60527 |   GET     /a/Apple/b/Banana
[GIN] 2015/08/13 - 16:06:05 | 404 |       3.229碌s | 10.10.10.1:60528 |   GET     /a/Apple/b/
/a/:a/b/:b route invoked. a="", b="Boat"
[GIN] 2015/08/13 - 16:06:17 | 200 |    1.077709ms | 10.10.10.1:60529 |   GET     /a//b/Boat
[GIN] 2015/08/13 - 16:06:28 | 404 |       3.646碌s | 10.10.10.1:60530 |   GET     /a//b/

@rawoke083 yes, it can be done.

[GIN-debug] GET   /product/:id/             --> main.handler (3 handlers)
[GIN-debug] GET   /product/:id/shop/:shopid --> main.handler (3 handlers)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

frederikhors picture frederikhors  路  3Comments

cxk280 picture cxk280  路  3Comments

Bloomca picture Bloomca  路  3Comments

xpbliss picture xpbliss  路  3Comments

gplume picture gplume  路  3Comments