第一步:
go get github.com/gin-gonic/gin
假如不出现意外的话,文档目录结构应该是这样的
github.com
---gin-contrib
---gin-gonic
--- golang
---go-playground
---leodido
---mattn
---ugorji
gopkg.in
---go-playground
---yaml.v2
然后,我们新建一个test.go文件运行 go run test.go,看看是否安装成功
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello World!")
})
router.Run(":8000")
}
如果你看到这样的话,说明你成功了
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET / --> main.main.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8000
[GIN] 2019/05/29 - 05:46:56 | 200 | 0s | 127.0.0.1 | GET /
[GIN] 2019/05/29 - 05:46:56 | 404 | 0s | 127.0.0.1 | GET /favicon.ico
如果你看到如下错误
can't load package: package src/gopkg.in/go-playground/validator.v8: cannot find package "src/gopkg.in/go-playground/validator.v8" in any of:
D:\Go\src\gopkg.in\go-playground\validator.v8 (from $GOROOT)
D:\Go_Gin_TEST\src\gopkg.in\go-playground\validator.v8 (from $GOPATH)
或
src\gopkg.in\go-playground\validator.v8\errors.go:9:2: cannot find package "github.com/go-playground/universal-translator" in any of:
D:\Go\src\github.com\go-playground\universal-translator (from $GOROOT)
D:\Go_Project\src\github.com\go-playground\universal-translator (from $GOPATH)
src\gopkg.in\go-playground\validator.v8\baked_in.go:18:2: cannot find package "github.com/leodido/go-urn" in any of:
D:\Go\src\github.com\leodido\go-urn (from $GOROOT)
D:\Go_Project\src\github.com\leodido\go-urn (from $GOPATH)
或
src\gopkg.in\go-playground\universal-translator\errors.go:7:2: cannot find package "github.com/go-playground/locales" in any of:
D:\Go\src\github.com\go-playground\locales (from $GOROOT)
D:\Go_Project\src\github.com\go-playground\locales (from $GOPATH)
先不慌, 先去github上下载
如:
$ git clone git@github.com:go-playground/validator.git
Cloning into 'validator'...
remote: Enumerating objects: 84, done.
remote: Counting objects: 100% (84/84), done.
remote: Compressing objects: 100% (47/47), done.
remote: Total 2459 (delta 51), reused 70 (delta 37), pack-reused 2375
Receiving objects: 100% (2459/2459), 1.26 MiB | 810.00 KiB/s, done.
Resolving deltas: 100% (1524/1524), done.
下载完后,把文件目录validator -->validator.v8
同理,上面找不到package都可以用此方法
如果你又出现了
# github.com/gin-gonic/gin/binding
src\github.com\gin-gonic\gin\binding\default_validator.go:48:14: undefined: validator.Config
src\github.com\gin-gonic\gin\binding\default_validator.go:49:29: too many arguments in call to validator.New
这是 validator 的版本和 gin 的不匹配,使用 go get -u gopkg.in/go-playground/validator.v8 或者删掉目录 使用 go get gopkg.in/go-playground/validator.v8
问题不大!