gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
78.93k stars 8.02k forks source link

Parameters in path API example in README doesnt work for POST #2490

Open shashipratap opened 4 years ago

shashipratap commented 4 years ago

Description

while running the example of parameters in Path as given in readme.md it fails with error c.FullPath() == "/user/:name/*action" evaluated but not used

How to reproduce

create a file example.go as below and run it with command go run example.go

`package main

import ( "github.com/gin-gonic/gin" "net/http" "fmt" )

func main() { router := gin.Default()

    // This handler will match /user/john but will not match /user/ or /user
    router.GET("/user/:name", func(c *gin.Context) {
            name := c.Param("name")
            c.String(http.StatusOK, "Hello %s", name)
    })

    // However, this one will match /user/john/ and also /user/john/send
    // If no other routers match /user/john, it will redirect to /user/john/
    router.GET("/user/:name/*action", func(c *gin.Context) {
            name := c.Param("name")
            action := c.Param("action")
            message := name + " is " + action
            c.String(http.StatusOK, message)
    })

    // For each matched request Context will hold the route definition
    router.POST("/user/:name/*action", func(c *gin.Context) {
             c.FullPath() == "/user/:name/*action" // true

    })

    router.Run(":8090")

} `

Expectations

go run example.go should run without errors and README updated accordingly with some explanation about fullpath and context

Environment

2HgO commented 4 years ago

This is not a gin issue. The line c.FullPath() == "/user/:name/*action" evaluates to an unused value and so must be discarded or used. Otherwise, the go compiler will not build the application.

shashipratap commented 4 years ago

This is not a gin issue. The line c.FullPath() == "/user/:name/*action" evaluates to an unused value and so must be discarded or used. Otherwise, the go compiler will not build the application.

Agree , but shouldn't we write code in examples that actually works , just to improve readme.md