供应商文件夹在使用’go build’时未被使用。

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

vendor folder not used with 'go build'

问题

我正在使用MacOS Sierra中的go-1.7版本。

我的项目位于*$GOPATH/src文件夹中,并且在其中有一个vendor*文件夹,其中包含所有的依赖项。

我在代码中使用以下方式引用依赖项:

import (
"github.com/google/go-github/github"
)

现在,如果我运行go build,会得到一个消息,说找不到我使用的所有依赖项在$GOROOT和$GOPATH中,但是将"vendor"添加到我的代码中可以工作:

import (
"vendor/github.com/google/go-github/github"
)

但据我所了解,应该可以像第一个代码片段中那样做。

额外信息:没有符号链接等。

英文:

I'm using go-1.7 in MacOS Sierra.

My project is inside my $GOPATH/src folder and has a vendor folder inside with all its dependencies.

and I'm using the dependencies like this inside my code:

import (
"github.com/google/go-github/github"
)

Now if I run go build i get the message that all my dependencies I use could not be found inside the $GOROOT and $GOPATH on the other and adding "vendor" to my code is working:

import (
"vendor/github.com/google/go-github/github"
)

But as far as I understood it should be possible to to like in the first code snipped.

ah FYI there are no symlinks etc.

答案1

得分: 14

从go 1.12+开始,go模块是处理依赖关系的新方法。

  • 修复依赖版本:go mod init
  • 将模块放入vendor文件夹:go mod vendor
  • 从vendor目录构建:go build -mod vendor -o output
英文:

From go 1.12+ go modules is the new way of handling dependencies.

  • To fix dependency version go mod init
  • To bring modules in vendor folder go mod vendor
  • To build from the vendor directory go build -mod vendor -o output

答案2

得分: 5

好的,以下是翻译好的内容:

好的,找到了问题:

我的 MacOS 文件系统是不区分大小写的,但是看起来 go 工具无法处理这个问题...
修复了我的 $GOPATH,现在它可以正常工作了...

英文:

Ok found the problem:

My MacOS File System is Case Insensitive but it looks like the go tools cant handle that...
fixed my $GOPATH and now it works like it should...

答案3

得分: 2

你可以在终端中使用echo $GOPATH命令来查看你的Golang路径,在我的情况下是/home/gujarat/golang。这是默认路径,不包括src路径。

所以从这里开始,你的所有包和依赖项都在$GOPATHsrc文件夹中。例如,这是我在项目中使用的一些包。

"fmt"
"github.com/myproject/lol/src/config" // 注意第一个github.com
"gopkg.in/redis.v4" // 注意gopkg.in
"log"

从上面的包中,你必须将所有的文件夹和依赖项复制到你的src文件夹中,比如github.comgopkg.in,这些文件夹必须存在于src的根目录中。

如果你无法导入"github.com/google/go-github/github",这意味着你的src中的github.com文件夹没有这个文件夹。希望对你有所帮助。

英文:

you could use echo $GOPATH in your terminal to see your path of golang in my case is /home/gujarat/golang. this is the default path without src path.

So from here all your packages and dependencies are inside src in $GOPATH. for example here is some packages that I used in my project.

"fmt"
"github.com/myproject/lol/src/config" // notice the first github.com
"gopkg.in/redis.v4" // notice the gopkg.in
"log"

from the above package you must have all the folder and dependencies copied in your src folder. like github.com and gopkg.in these are the folder that must exist if in src root.

and if you cant import your "github.com/google/go-github/github" it means that your github.com inside your src doens't have this folder. hope it helps

答案4

得分: 2

我遇到了这个错误,苦苦挣扎了一段时间;我升级到了Go 1.8.3,并且使用的是glide版本0.12.3;我的GOPATH设置为$PWD,并且glide将所有内容安装在./vendor文件夹中。

最后找到了错误的原因;GOPATH下应该有一个src文件夹,你的所有工作空间都应该在这个src文件夹下

这应该在文档中有提到,在开发机器上我设置得正确;但在Jenkins构建中忽略了这一点。

英文:

I got this error and struggled for a while; I update to Go 1.8.3 and was using glide version 0.12.3; and my GOPATH was set as $PWD and glide was installing everything in ./vendor folder;

Finally got the error; THERE SHOULD BE a src folder under GOPATH and all your workspace should be under this src folder

It must be in the docs, and in dev machine I set it proper; but missed in Jenkins build

huangapple
  • 本文由 发表于 2016年12月25日 16:09:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/41319633.html
匿名

发表评论

匿名网友

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

确定