Go Gin安装

第一步:

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

问题不大!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值