我试着使用Gin,它是Golang的框架。
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)
有人知道我怎么解决这个问题吗?
*中心7
·go版本go1.6.3 linux/amd64 64
编辑:
我实际上使用滑翔,但我更新了杜松子酒为全球。并且更新到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)
发布于 2016-08-16 09:07:41
编辑: OP有“供应商dir创建的幻灯片”与旧版本的软件包。并通过删除该文件夹(更新供应商包)解决问题。
备注:go get
从不检出或更新存储在供应商目录中的代码。
c.Param(key)
是c.Params.ByName(key)
的快捷方式,请参阅c.Param(key)
文档:
// param返回URL Param的值。//它是c.Params.ByName(key) // router.GET("/user/: id“、func(c *gin.Context) {/a GET请求到/user // id := c.Param("id") /id == "john”/ }) func (c *上下文)Param(键字符串)字符串{返回c.Params.ByName(键)}的快捷方式。
您需要更新github.com/gin-gonic/gin
包,请尝试:
go get -u github.com/gin-gonic/gin
并确保没有任何vendor
,并尝试删除除main.go
以外的所有文件和供应商dir,然后删除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
发布于 2016-08-16 08:49:16
我已经开始工作了,但是我不得不导入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
为我返回go version go1.5.3 linux/amd64
。
您的错误似乎出现在您的gin.go的第19行,如'./main.go:19: c.Param未定义的‘脚本中的其他行是什么?
提供的脚本只有14行长。
发布于 2016-08-17 01:20:58
我成功了。
原因是我的目录深度。我将main.go放在"/go/myproject/src/server/main.go“,并设置为"export =/go/myproject”。
在这种情况下,我得到了错误。但是当我在"/go/myproject/src/ main.go“更改了main.go的路径后,它就可以工作了。
我想知道我没有认为我的目录结构是错误的。我可以把main.go放任何深度,不是吗?
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")
}
但是当我不调用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设置在相同的位置。
https://stackoverflow.com/questions/38970180
复制相似问题