Golang Gin“c.Param undefined(type * gin.Context没有字段或方法Param)” [英] Golang Gin "c.Param undefined (type *gin.Context has no field or method Param)"

查看:2213
本文介绍了Golang Gin“c.Param undefined(type * gin.Context没有字段或方法Param)”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Golang框架的杜松子酒。

https:/ /github.com/gin-gonic/gin



我从官方github复制了示例代码。

就是这样。

 包主

导入(
github.com/gin-gonic/gin
net / http


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

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

router.Run(:8080)
}

但我得到了错误。

 #go run main.go 
#命令行参数./main.go:12:c。 Param undefined(type * gin.Context没有字段或方法Param)

有谁知道我该怎么做修复这个问题?

CentOS7
$ b $ go版本go1.6.3 linux / amd64 $ /

编辑:



我实际上使用滑翔,但我更新了杜松子酒作为全球。
并且更新到1.7,但仍然出现相同的错误:

 #go get -u -v github。 com / gin-gonic / gin 
github.com/gin-gonic/gin(下载)
github.com/golang/protobuf(下载)
提取https://gopkg.in/ go-playground / validator.v8?go-get = 1
https获取失败:获取https://gopkg.in/go-playground/validator.v8?go-get=1:拨号tcp:lookup gopkg。在192.168.11.1:53上:拨号udp 192.168.11.1:53:I / O超时
gopkg.in/go-playground/validator.v8(下载)
正在获取https://gopkg.in /yaml.v2?go-get=1
https获取失败:在192.168.11.1:53获取https://gopkg.in/yaml.v2?go-get=1:拨打tcp:lookup gopkg.in :拨号udp 192.168.11.1:53:I / O超时
gopkg.in/yaml.v2(下载)
github.com/manucorporat/sse(下载)
获取https:// golang.org/x/net/context?go-get=1
从https://golang.org/x/net/context?go-get=1(状态码200)解析元标记
得到golang.org/x/net/context:找到元标签main.metaImport {前缀:golan g.org/x/net,VCS:git,RepoRoot:https://go.googlesource.com/net} https://golang.org/x/net/context?go-get= 1
获取golang.org/x/net/context:验证非权威元标记
获取https://golang.org/x/net?go-get=1
从https://golang.org/x/net?go-get=1(状态码200)解析元标记
golang.org/x/net(下载)

#go版本
go版本go1.7 linux / amd64

#go run test.go
#命令行参数
./test.go:12:c .Param未定义(类型* gin.Context没有字段或方法参数)


解决方案<编辑:
OP有由Glide创建的供应商目录与旧版本的软件包。
,并且通过删除该文件夹(更新供应商包)解决了问题。

注意 go get 绝不会检出或更新存储在供应商目录中的代码。






c.Param(key) c.Params .ByName(key),参见 c.Param(key)文件:


  // // Param返回URL参数的值。 
//这是c.Params.ByName(key)的快捷方式
// router.GET(/ user /:id,func(c * gin.Context){
// //一个GET请求到/ user / john
// id:= c.Param(id)// id ==john
//})
func (c * Context)Param(key string)string {
return c.Params.ByName(key)
}







您需要更新 github.com/gin-gonic/gin package,试试:

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

并确保没有任何供应商并尝试全部删除文件和供应商目录,除 main.go 然后 go build (或更新您的供应商软件包)。






您的代码在 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)
}

在浏览器中打开 http://127.0.0.1:8080/user/World


输出:

  Hello World 


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)

解决方案

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

这篇关于Golang Gin“c.Param undefined(type * gin.Context没有字段或方法Param)”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆