我遇到了一个错误,即供应商包没有被 go 正确解析;在 macOS High Sierra 10.13.6 上。我将使用 github.com/gorilla/mux 作为示例包
$ echo $GOPATH
/Users/gregorysims/go
$ go version
go version go1.11 darwin/amd64
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/gregorysims/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/gregorysims/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/tb/6c5vksm558q5q4t0fxfscgy80000gp/T/go-build409352678=/tmp/go-build -gno-record-gcc-switches -fno-common"
我正在导入包
import (
...
"github.com/gorilla/mux"
...
)
运行时:
go build -o out main.go
对于供应商中的每个包,我都收到以下错误
main.go:6:2: cannot find package "_/Users/gregorysims/go/src/site/user/project/vendor/github.com/gorilla/mux" in any of:
/usr/local/go/src/_/Users/gregorysims/go/src/site/user/project/vendor/github.com/gorilla/mux (from $GOROOT)
/Users/gregorysims/go/src/_/Users/gregorysims/go/src/site/user/project/vendor/github.com/gorilla/mux (from $GOPATH)
该文件夹应位于
/Users/gregorysims/go/src/site/user/project/vendor/github.com/gorilla/mux
并且存在于目录中。
为什么要在下面加上前缀?
/Users/gregorysims/go/src/_
更新
删除本地项目并从远程重新克隆后,我尝试在没有下载任何依赖项的情况下进行构建。这有预期的输出:
main.go:6:2: cannot find package "github.com/gorilla/mux" in any of:
/usr/local/go/src/github.com/gorilla/mux (from $GOROOT)
/Users/gregorysims/go/src/github.com/gorilla/mux (from $GOPATH)
只有在运行构建命令之后才会产生上述错误dep ensure
。
更新 2
创建一个空的供应商目录会将输出更改为:
main.go:6:2: cannot find package "github.com/gorilla/mux" in any of:
/Users/gregorysims/go/src/site/user/project/vendor/github.com/gorilla/mux (vendor tree)
/usr/local/go/src/github.com/gorilla/mux (from $GOROOT)
/Users/gregorysims/go/src/github.com/gorilla/mux (from $GOPATH)
更新 3
为了模拟我调用的 dep,go get github.com/gorilla/mux
然后是:
mv $GOPATH/src/github.com/gorilla/mux $GOPATH/src/site/user/project/vendor/github.com/gorilla/mux
这样做之后,问题再次浮出水面。