戈兰杜松子酒 "c.Param undefined (type *gin.Context has no field or method Param)"
Golang Gin "c.Param undefined (type *gin.Context has no field or method Param)"
我尝试使用作为 Golang 框架的 Gin。
https://github.com/gin-gonic/gin
并且我从官方github复制了示例代码。
就像这样。
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")
}
但是我得到了错误。
# go run main.go
# command-line-arguments ./main.go:12: c.Param undefined (type *gin.Context has no field or method Param)
有谁知道我该如何解决这个问题?
・CentOS7
・go版本go1.6.3linux/amd64
编辑:
我实际上使用了 glide,但我将 gin 更新为全局。
并将 Go 更新到 1.7 但仍然出现相同的错误:
# 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)
我成功了,但我必须导入 http 包:
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
对我来说。
根据'./main.go:19: c.Param undefined',您的错误似乎出现在 gin.go 的第 19 行,脚本中的其他行是什么?
提供的脚本只有 14 行。
编辑:
OP 有 "vendor dir created by the Glide" 旧版本的包。
并通过删除该文件夹(更新供应商包)解决了问题。
Note:go get
从不检出或更新存储在供应商目录中的代码。
c.Param(key)
是 c.Params.ByName(key)
的快捷方式,参见 c.Param(key)
文档:
// 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)
}
您需要更新 github.com/gin-gonic/gin
包,试试:
go get -u github.com/gin-gonic/gin
并确保没有任何 vendor
并尝试删除除 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
成功了
原因是我的目录深度。
我把 main.go 放在 "/go/myproject/src/server/main.go" 并设置 "export GOPATH=/go/myproject".
在这种情况下,我得到了错误。
但是当我在“/go/myproject/src/main.go”处更改main.go的路径后,它起作用了。
我纳闷了,没想到是我的目录结构不对。
我可以把 main.go 放在任意深度,不是吗?
已添加 2
# 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")
}
但是当我不调用 param 函数时,像这样,Gin 看起来工作正常。
# 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
main.go设置在同一个地方
遇到同样的问题。 Glide 现在尝试使用 Semantic Versioning 2.0.0,glide 已经有一段时间没有更新了,手动将版本设置为最新的 develop 修复了这个问题:
- package: github.com/gin-gonic/gin
version: f931d1ea80ae95a6fc739213cdd9399bd2967fb6
我运行遇到了同样的问题。
我不确定根本原因,但我通过更改
中的代码来修复它
c.Param("name")
到
c.Params.ByName("name")
如果您查看 Param()
方法的源代码,这就是它在幕后调用的内容。
我尝试使用作为 Golang 框架的 Gin。
https://github.com/gin-gonic/gin
并且我从官方github复制了示例代码。
就像这样。
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")
}
但是我得到了错误。
# go run main.go
# command-line-arguments ./main.go:12: c.Param undefined (type *gin.Context has no field or method Param)
有谁知道我该如何解决这个问题?
・CentOS7
・go版本go1.6.3linux/amd64
编辑:
我实际上使用了 glide,但我将 gin 更新为全局。 并将 Go 更新到 1.7 但仍然出现相同的错误:
# 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)
我成功了,但我必须导入 http 包:
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
对我来说。
根据'./main.go:19: c.Param undefined',您的错误似乎出现在 gin.go 的第 19 行,脚本中的其他行是什么?
提供的脚本只有 14 行。
编辑: OP 有 "vendor dir created by the Glide" 旧版本的包。 并通过删除该文件夹(更新供应商包)解决了问题。
Note:go get
从不检出或更新存储在供应商目录中的代码。
c.Param(key)
是 c.Params.ByName(key)
的快捷方式,参见 c.Param(key)
文档:
// 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) }
您需要更新 github.com/gin-gonic/gin
包,试试:
go get -u github.com/gin-gonic/gin
并确保没有任何 vendor
并尝试删除除 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
成功了
原因是我的目录深度。 我把 main.go 放在 "/go/myproject/src/server/main.go" 并设置 "export GOPATH=/go/myproject".
在这种情况下,我得到了错误。 但是当我在“/go/myproject/src/main.go”处更改main.go的路径后,它起作用了。
我纳闷了,没想到是我的目录结构不对。 我可以把 main.go 放在任意深度,不是吗?
已添加 2
# 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")
}
但是当我不调用 param 函数时,像这样,Gin 看起来工作正常。
# 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
main.go设置在同一个地方
遇到同样的问题。 Glide 现在尝试使用 Semantic Versioning 2.0.0,glide 已经有一段时间没有更新了,手动将版本设置为最新的 develop 修复了这个问题:
- package: github.com/gin-gonic/gin
version: f931d1ea80ae95a6fc739213cdd9399bd2967fb6
我运行遇到了同样的问题。
我不确定根本原因,但我通过更改
c.Param("name")
到
c.Params.ByName("name")
如果您查看 Param()
方法的源代码,这就是它在幕后调用的内容。