Gin: routeur deux paramètres

Créé le 11 août 2015  ·  3Commentaires  ·  Source: gin-gonic/gin

SALUT
Est-il possible de faire un itinéraire comme celui-ci

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

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

Tous les 3 commentaires

Personne :( ?

Oui.

charrington<strong i="6">@localhost</strong> ~/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<strong i="7">@localhost</strong> ~/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 oui, cela peut être fait.

[GIN-debug] GET   /product/:id/             --> main.handler (3 handlers)
[GIN-debug] GET   /product/:id/shop/:shopid --> main.handler (3 handlers)
Cette page vous a été utile?
0 / 5 - 0 notes

Questions connexes

oryband picture oryband  ·  3Commentaires

ccaza picture ccaza  ·  3Commentaires

nxvl picture nxvl  ·  3Commentaires

olegsobchuk picture olegsobchuk  ·  3Commentaires

iiinsomnia picture iiinsomnia  ·  3Commentaires