gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:42:01 26 4
gpt4 key购买 nike

我尝试使用作为 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.3 linux/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)

最佳答案

编辑:OP 有“由 Glide 创建的 vendor 目录”和旧版本的包。并通过删除该文件夹(更新 vendor 包)解决了问题。

Note : go get 从不检查或更新存储在 vendor 目录中的代码。


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 之外的所有文件和 vendor 目录,然后 go build (或更新您的 vendor 包)。


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

关于Golang Gin "c.Param undefined (type *gin.Context has no field or method Param)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38970180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com