go import导入的理解

2018-12-08  本文已影响171人  吴世浩

在使用go的时候如果依赖导入github上的,比如下面样式

import "github.com/go-sql-driver/mysql"

我们需要先执行get操作

go get github.com/go-sql-driver/mysql

它会下载到你的gopath目录下

我看的项目是这个
https://blog.csdn.net/u010649766/article/details/79458004

GOROOT=/usr/local/go #gosetup
GOPATH=/mouse/study/goSpace/goTestSpace #gosetup
/usr/local/go/bin/go build -i -o /private/var/folders/6y/dnlwm8zn67d_10p7qjmml1r40000gn/T/___go_build_main_go /mouse/study/ownerWorkSpace/go/goTest/tlsdemo/demo/main.go #gosetup
../../../../goSpace/goTestSpace/src/github.com/gin-gonic/gin/binding/protobuf.go:11:2: cannot find package "github.com/golang/protobuf/proto" in any of:
    /mouse/study/goSpace/goTestSpace/src/github.com/gin-gonic/gin/vendor/github.com/golang/protobuf/proto (vendor tree)
    /usr/local/go/src/github.com/golang/protobuf/proto (from $GOROOT)
    /mouse/study/goSpace/goTestSpace/src/github.com/golang/protobuf/proto (from $GOPATH)
../../../../goSpace/goTestSpace/src/github.com/gin-gonic/gin/logger.go:14:2: cannot find package "github.com/mattn/go-isatty" in any of:
    /mouse/study/goSpace/goTestSpace/src/github.com/gin-gonic/gin/vendor/github.com/mattn/go-isatty (vendor tree)
    /usr/local/go/src/github.com/mattn/go-isatty (from $GOROOT)
    /mouse/study/goSpace/goTestSpace/src/github.com/mattn/go-isatty (from $GOPATH)
../../../../goSpace/goTestSpace/src/github.com/gin-gonic/gin/binding/msgpack.go:12:2: cannot find package "github.com/ugorji/go/codec" in any of:
    /mouse/study/goSpace/goTestSpace/src/github.com/gin-gonic/gin/vendor/github.com/ugorji/go/codec (vendor tree)
    /usr/local/go/src/github.com/ugorji/go/codec (from $GOROOT)
    /mouse/study/goSpace/goTestSpace/src/github.com/ugorji/go/codec (from $GOPATH)
../../../../goSpace/goTestSpace/src/github.com/gin-gonic/gin/binding/default_validator.go:11:2: cannot find package "gopkg.in/go-playground/validator.v8" in any of:
    /mouse/study/goSpace/goTestSpace/src/github.com/gin-gonic/gin/vendor/gopkg.in/go-playground/validator.v8 (vendor tree)
    /usr/local/go/src/gopkg.in/go-playground/validator.v8 (from $GOROOT)
    /mouse/study/goSpace/goTestSpace/src/gopkg.in/go-playground/validator.v8 (from $GOPATH)
../../../../goSpace/goTestSpace/src/github.com/gin-gonic/gin/binding/yaml.go:12:2: cannot find package "gopkg.in/yaml.v2" in any of:
    /mouse/study/goSpace/goTestSpace/src/github.com/gin-gonic/gin/vendor/gopkg.in/yaml.v2 (vendor tree)
    /usr/local/go/src/gopkg.in/yaml.v2 (from $GOROOT)
    /mouse/study/goSpace/goTestSpace/src/gopkg.in/yaml.v2 (from $GOPATH)

Compilation finished with exit code 1

刚刚跑一个demo看下怎么连接数据测试下,发现上述错误,其实这个错误也让我对gopath认识加深了,我们看看某一个错误

/mouse/study/goSpace/goTestSpace/src/github.com/gin-gonic/gin/vendor/github.com/golang/protobuf/proto (vendor tree)
/usr/local/go/src/github.com/golang/protobuf/proto (from $GOROOT)
/mouse/study/goSpace/goTestSpace/src/github.com/golang/protobuf/proto (from $GOPATH)

在我们引入第三方的时候,其会在三个地方区查找
1、GOROOT路径
2、GOPATH路径
3、在原目录中的vendor目录下进行查找

上一篇下一篇

猜你喜欢

热点阅读