Golang Gin “c.Param undefined (type *gin.Context has no field or method Param)”

廉价感情. 提交于 2019-12-24 00:42:58

问题


I tried to use the Gin which is framework for Golang.
https://github.com/gin-gonic/gin

And the I copied sample codes from official github.
It's like this.

package main

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

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

    router.GET("/user/:name", func(c *gin.Context) {
        name := c.Param("name")
        c.String(http.StatusOK, "Hello %s", name)
        })

    router.Run(":8080")
}

But I got the error.

# go run main.go
# command-line-arguments ./main.go:12: c.Param undefined (type *gin.Context has no field or method Param)

Does anyone know how can I fix the problem ?

・CentOS7

・go version go1.6.3 linux/amd64

Edit:

I actually use the glide but I updated gin as global. And also update Go to 1.7 but getting still the same error:

# go get -u -v github.com/gin-gonic/gin
github.com/gin-gonic/gin (download)
github.com/golang/protobuf (download)
Fetching https://gopkg.in/go-playground/validator.v8?go-get=1
https fetch failed: Get https://gopkg.in/go-playground/validator.v8?go-get=1: dial tcp: lookup gopkg.in on 192.168.11.1:53: dial udp 192.168.11.1:53: i/o timeout
gopkg.in/go-playground/validator.v8 (download)
Fetching https://gopkg.in/yaml.v2?go-get=1
https fetch failed: Get https://gopkg.in/yaml.v2?go-get=1: dial tcp: lookup gopkg.in on 192.168.11.1:53: dial udp 192.168.11.1:53: i/o timeout
gopkg.in/yaml.v2 (download)
github.com/manucorporat/sse (download)
Fetching https://golang.org/x/net/context?go-get=1
Parsing meta tags from https://golang.org/x/net/context?go-get=1 (status code 200)
get "golang.org/x/net/context": found meta tag main.metaImport{Prefix:"golang.org/x/net", VCS:"git", RepoRoot:"https://go.googlesource.com/net"} at https://golang.org/x/net/context?go-get=1
get "golang.org/x/net/context": verifying non-authoritative meta tag
Fetching https://golang.org/x/net?go-get=1
Parsing meta tags from https://golang.org/x/net?go-get=1 (status code 200)
golang.org/x/net (download)

# go version
go version go1.7 linux/amd64

# go run test.go
# command-line-arguments
./test.go:12: c.Param undefined (type *gin.Context has no field or method Param)

回答1:


Edit: The OP had "vendor dir created by the Glide" with old version of package. and problem solved by removing that folder (updating vendor package).

Note: go get never checks out or updates code stored in vendor directories.


c.Param(key) is a shortcut for c.Params.ByName(key), see c.Param(key) Docs:

// Param returns the value of the URL param.
// It is a shortcut for c.Params.ByName(key)
//        router.GET("/user/:id", func(c *gin.Context) {
//            // a GET request to /user/john
//            id := c.Param("id") // id == "john"
//        })
func (c *Context) Param(key string) string {
  return c.Params.ByName(key)
}

You need to update github.com/gin-gonic/gin package, try:

go get -u github.com/gin-gonic/gin

And make sure there aren't any vendor and try remove all files and vendor dir except main.go then go build (or update your vendor package).


Your code works fine in go1.7:

package main

import (
    "net/http"

    "github.com/gin-gonic/gin"
)

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

    router.GET("/user/:name", func(c *gin.Context) {
        name := c.Param("name")
        c.String(http.StatusOK, "Hello %s", name)
    })

    router.Run(":8080")
}

Open in browser http://127.0.0.1:8080/user/World
output:

Hello World



回答2:


I got this working, but I had to import the http package:

package main

import "github.com/gin-gonic/gin"
import "net/http"

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

router.GET("/user/:name", func(c *gin.Context) {
    name := c.Param("name")
    c.String(http.StatusOK, "Hello %s", name)
    })

router.Run(":8080")
}

go version returns go version go1.5.3 linux/amd64 for me.

It seems your error was on line 19 of your gin.go as per './main.go:19: c.Param undefined' what are the other lines in the script?

The provided script is only 14 lines long.




回答3:


I got the success.

The reason why is my directory depth. I put the main.go at "/go/myproject/src/server/main.go" and set "export GOPATH=/go/myproject".

In this case, I got the error. But after I changed the path of main.go at "/go/myproject/src/main.go", it works.

I was wondering that I didn't think that my directory structure is wrong. I can put the main.go any depth, can't I ?

Added2

# pwd
/go/myproject/src/server
# ls -l
total 12
-rw-r--r-- 1 1000 ftp 633  8月 17 02:52 glide.lock
-rw-r--r-- 1 1000 ftp  78  8月 17 02:51 glide.yaml
-rw-r--r-- 1 1000 ftp 254  8月 17 02:51 main.go
drwxr-xr-x 1 1000 ftp 136  8月 17 02:52 vendor
# go run main.go
# command-line-arguments
./main.go:12: c.Param undefined (type *gin.Context has no field or method Param)
# cat main.go
package main

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

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

        router.GET("/user/:name", func(c *gin.Context) {
            name := c.Param("name")
            c.String(http.StatusOK, "Hello %s", name)
        })

        router.Run(":8080")
}

But when I don't call the param function, like this , Gin looks working.

# cat main.go
package main

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

func main() {
        r := gin.Default()
        r.GET("/ping", func(c *gin.Context) {
            c.JSON(200, gin.H{
                "message": "pong",
            })
        })
        r.Run(":8080")
}
# go run main.go
[GIN-debug] GET   /ping                     --> main.main.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2016/08/17 - 03:01:17 | 200 |     79.238µs | [::1]:41546 |   GET     /ping

The main.go is set at same place.




回答4:


Got the same problem. Glide now tries to use Semantic Versioning 2.0.0, which glide hasn't update in a while, setting the version manually to latest develop fixes the issue:

- package: github.com/gin-gonic/gin
  version: f931d1ea80ae95a6fc739213cdd9399bd2967fb6



回答5:


I ran into the same issue.
I'm not sure the root cause but I fixed it by changing the code from

c.Param("name")

to

c.Params.ByName("name")

If you look at the source for the Param() method, this is what it called under the covers.



来源:https://stackoverflow.com/questions/38970180/golang-gin-c-param-undefined-type-gin-context-has-no-field-or-method-param

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!