Golang 无法找到/使用 vendor 文件夹。

huangapple go评论96阅读模式
英文:

Golang cannot find/use vendor folder

问题

有人知道为什么在导入github.com/juju/errors时,我在$GOPATH$GOROOT前面得到了一个_吗?

仓库结构如下:

-$GOPATH/src/github.com/codelingo/lexicon/vendor
-$GOPATH/src/github.com/codelingo/lexicon/codelingo/ast/go/src/main.go
-$GOPATH/src/github.com/codelingo/lexicon/codelingo/ast/go/src/node/node.go

带有行号的main.go:

1 package main
2
3 import (
4	"encoding/json"
5	"fmt"
6	"os"
7	"strings"
8
9	"github.com/juju/errors"
10
11	"./key"
12	"./node"
13	"./parser"
14	"./property"
15	"./util"
16 )
// main.go的其余部分

带有行号的node.go:

1 package node
2
3 import (
4 	"encoding/json"
5	"github.com/juju/errors"
6	"reflect"
7)
// node.go的其余部分

运行$ go run main.go时出现以下错误:

node/node.go:5:2: cannot find package "_/home/jzhu/go/src/github.com/codelingo/lexicon/codelingo/ast/go/src/vendor/github.com/juju/errors" in any of:
    /usr/local/go/src/_/home/jzhu/go/src/github.com/codelingo/lexicon/codelingo/ast/go/src/vendor/github.com/juju/errors (from $GOROOT)
    /home/jzhu/go/src/_/home/jzhu/go/src/github.com/codelingo/lexicon/codelingo/ast/go/src/vendor/github.com/juju/errors (from $GOPATH)

vendor文件夹包含上述包,main.go可以使用它(例如import "github.com/juju/errors")。

vendor文件夹位于我的$GOPATH(/home/jzhu/go)下。

注意:在包路径前面有一个奇怪的“_”。

我期望的结果应该是查找vendor树,如下所示:

vendor/github.com/codelingo/lingo/service/server/service.go:10:2: cannot find package "github.com/codelingo/platform/controller/graphdb/query/result" in any of:
	/home/jzhu/go/src/github.com/codelingo/platform/vendor/github.com/codelingo/platform/controller/graphdb/query/result (vendor tree)
	/home/jzhu/go/src/github.com/vendor/github.com/codelingo/platform/controller/graphdb/query/result
	/usr/local/go/src/github.com/codelingo/platform/controller/graphdb/query/result (from $GOROOT)
	/home/jzhu/go/src/github.com/codelingo/platform/controller/graphdb/query/result (from $GOPATH)

环境:

$go env

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/jzhu/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build894537960=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

$ go version
go version go1.8.3 linux/amd64

$ glide --version
glide version 0.13.0-dev
英文:

Does anyone have a clue about why do I get an _in front of the $GOPATH and $GOROOT when I import github.com/juju/errors e.g.

repo structure

-$GOPATH/src/github.com/codelingo/lexicon/vendor
-$GOPATH/src/github.com/codelingo/lexicon/codelingo/ast/go/src/main.go
-$GOPATH/src/github.com/codelingo/lexicon/codelingo/ast/go/src/node/node.go

main.go with line number

1 package main
2
3 import (
4	"encoding/json"
5	"fmt"
6	"os"
7	"strings"
8
9	"github.com/juju/errors"
10
11	"./key"
12	"./node"
13	"./parser"
14	"./property"
15	"./util"
16 )
// rest of main.go

node.go with line number

1 package node
2
3 import (
4 	"encoding/json"
5	"github.com/juju/errors"
6	"reflect"
7)
//rest of node.go

$ go run main.go 
node/node.go:5:2: cannot find package "_/home/jzhu/go/src/github.com/codelingo/lexicon/codelingo/ast/go/src/vendor/github.com/juju/errors" in any of:
    /usr/local/go/src/_/home/jzhu/go/src/github.com/codelingo/lexicon/codelingo/ast/go/src/vendor/github.com/juju/errors (from $GOROOT)
    /home/jzhu/go/src/_/home/jzhu/go/src/github.com/codelingo/lexicon/codelingo/ast/go/src/vendor/github.com/juju/errors (from $GOPATH)

The vendor folder contains the above packages and main.go can use it (e.g. import ("github.com/juju/errors")).

The vendor folder is under my $GOPATH (/home/jzhu/go)

Note: there is a weird "_" in front of the package path.

I was expecting something as following which looks into the vendor tree.

vendor/github.com/codelingo/lingo/service/server/service.go:10:2: cannot find package "github.com/codelingo/platform/controller/graphdb/query/result" in any of:
	/home/jzhu/go/src/github.com/codelingo/platform/vendor/github.com/codelingo/platform/controller/graphdb/query/result (vendor tree)
	/home/jzhu/go/src/github.com/vendor/github.com/codelingo/platform/controller/graphdb/query/result
	/usr/local/go/src/github.com/codelingo/platform/controller/graphdb/query/result (from $GOROOT)
	/home/jzhu/go/src/github.com/codelingo/platform/controller/graphdb/query/result (from $GOPATH)

Environment:

$go env

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/jzhu/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build894537960=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

$ go version
go version go1.8.3 linux/amd64

$ glide --version
glide version 0.13.0-dev

答案1

得分: 1

解决方法是在导入时使用正确的相对路径来打包。
例如,在main.go中使用import "github.com/codelingo/lexicon/codelingo/ast/go/src/node"而不是import "./node"

https://github.com/Masterminds/glide/issues/602

英文:

The solution is to use the correct relative path to package in import.
e.g import "github.com/codelingo/lexicon/codelingo/ast/go/src/node" instead of import "./node" in main.go

https://github.com/Masterminds/glide/issues/602

huangapple
  • 本文由 发表于 2017年9月14日 08:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/46208854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定