Learning Go and using glide, vendor not being detected on go build

blov · · 856 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;ve tried Go before (<code>1.4</code>) but I got frustrated over dependency management so I stopped. Now I&#39;m relearning Go, I installed the latest Go version (<code>1.7.1</code>) using <a href="https://github.com/moovweb/gvm" rel="nofollow">gvm</a> and I am looking to build a simple rest api app using <a href="https://github.com/gin-gonic/gin" rel="nofollow">gin</a>. I installed it using <code>glide get https://github.com/gin-gonic/gin</code> (<a href="https://glide.sh/" rel="nofollow">glide</a>) and that created a &#34;vendor&#34; folder on my project root. Running my app though, <code>go run main.go</code>, I encounter this error</p> <pre><code>main.go:3:8: cannot find package &#34;github.com/gin-gonic/gin&#34; in any of: /home/yowmamasita/.gvm/gos/go1.6.3/src/github.com/gin-gonic/gin (from $GOROOT) /home/yowmamasita/.gvm/pkgsets/go1.6.3/global/src/github.com/gin-gonic/gin (from $GOPATH) </code></pre> <p>It is not resolving the &#34;vendor&#34; directory glide just created</p> <pre><code>. ├── glide.lock ├── glide.yaml ├── main.go ├── README.md └── vendor └── github.com └── gin-gonic └── gin </code></pre> <p>Not sure what&#39;s happening here, I thought after 1.5, it should be able to resolve imports from &#34;vendor&#34; directories without doing anything. I even added my projects folder on my <code>$GOPATH</code></p> <p><code>/home/yowmamasita/.gvm/pkgsets/go1.7.1/global:/home/yowmamasita/goprojects</code></p> <p>What am I doing wrong here? I tried 1.6.3 too and I get the same error.</p> <hr/>**评论:**<br/><br/>ChristophBerger: <pre><p>Why do you use <code>gvm</code>? Did the <a href="https://golang.org/doc/go1compat" rel="nofollow">Go 1 compatibility promise</a> not work out for you?</p> <p>I am asking because there seems to be a mismatch between the go version you use and the directories that <code>go run</code> searches for packages. I know you said you also tested with Go 1.6.3 but TBH a tool that messes with GOROOT and GOPATH is always a bit suspicious to me. </p> <p>To track down the issue,</p> <ul> <li>Does the output of <code>go env</code> look ok?<br/></li> <li>Does the glide yml file look ok?</li> <li>What happens if you get <code>gin</code> the classic way via <code>go get</code> (and try <code>go run</code> again)?</li> <li>What happens if you move the glide files (yml and lock) and the vendor dir to a safe location, then start over by going through the steps <a href="/u/philoserf" rel="nofollow">/u/philoserf</a> suggested (<code>glide init</code>, <code>glide up</code>), and finally do <code>glide get</code> again?</li> </ul></pre>yowmamasita: <pre><blockquote> <p>Why do you use <code>gvm</code>? Did the <a href="https://golang.org/doc/go1compat" rel="nofollow">Go 1 compatibility promise</a> not work out for you?</p> </blockquote> <p>I used <code>gvm</code> because it&#39;s easier to just use this to update go whenever a new version is out.</p> <blockquote> <p>Does the output of <code>go env</code> look ok? </p> </blockquote> <p><code>go env</code> and <code>go version</code> output: <a href="https://ghostbin.com/paste/ayebv" rel="nofollow">https://ghostbin.com/paste/ayebv</a></p> <blockquote> <p>Does the glide yml file look ok?</p> </blockquote> <p>not sure what you mean by &#34;okay&#34; but here&#39;s my glide.yaml: <a href="https://ghostbin.com/paste/3zsd2" rel="nofollow">https://ghostbin.com/paste/3zsd2</a></p> <blockquote> <p>What happens if you get <code>gin</code> the classic way via <code>go get</code> (and try <code>go run</code> again)?</p> </blockquote> <p>it works (the package is installed in <code>/home/yowmamasita/.gvm/pkgsets/go1.7.1/global/src/github.com/gin-gonic/gin</code>)</p> <blockquote> <p>What happens if you move the glide files (yml and lock) and the vendor dir to a safe location, then start over by going through the steps <a href="/u/philoserf" rel="nofollow">/u/philoserf</a> suggested (<code>glide init</code>, <code>glide up</code>), and finally do <code>glide get</code> again?</p> </blockquote> <p>Doesn&#39;t work again (removed gin installed globally)</p></pre>ChristophBerger: <pre><p>In the glide yaml file, the line <code>package: .</code> looks suspicious to me. I would have expected a <a href="https://glide.readthedocs.io/en/latest/glide.yaml/" rel="nofollow">path relative to $GOPATH/src</a> instead of the dot. Where is your main project located? It should reside in a folder beneath <code>$GOPATH/src</code>.</p></pre>yowmamasita: <pre><p>I think that&#39;s it. I don&#39;t have it under <code>$GOPATH/src</code>. It&#39;s just in my <code>$HOME/projects</code>. What should I put in my glide.yaml file? Or is it moving my project folder that is the proper way?</p> <p>Big thanks btw</p></pre>ChristophBerger: <pre><p>You need to move the project folder to $GOPATH/src. The Go tools are pretty strict about the location of source files. </p></pre>kaneshin: <pre><p>I use <a href="https://github.com/kaneshin/goenv" rel="nofollow">goenv</a> instead of gvm to manage the versions of golang.</p></pre>yowmamasita: <pre><p>will give this a try now, looks great as this doesn&#39;t modify my GOPATH as <a href="/u/ChristophBerger" rel="nofollow">/u/ChristophBerger</a> has pointed out with <code>gvm</code></p></pre>shovelpost: <pre><blockquote> <p>Now I&#39;m relearning Go</p> </blockquote> <p>If you are relearning Go then I&#39;d recommend this time to learn to do the procedure yourself.</p> <p>To update to a new version you delete the old folder, download the new version, unzip it and copy it to the old location. On Windows the procedure is even simpler.</p> <p>Now unfortunately we do not have a community blessed vendor tool yet. My personal recommendation is <a href="https://github.com/FiloSottile/gvt" rel="nofollow">gvt</a>. It&#39;s so simple it hurts.</p> <p>Still I would recommend before using a tool, to do the vendoring procedure a few times by hand to get a better feel of how things work.</p></pre>forgiv: <pre><p>The problem is that the go run command checks for the project to be in the Go src directory. Personally I hate using that directory, but if you don&#39;t mind not being able to use go run or go install, the go build command works in any directory.</p></pre>ghazninattarshah: <pre><p>I encountered same issue, that when you do go build, it doesn&#39;t detect the vendor directory created by glide. </p></pre>philoserf: <pre><p>Why use <code>glide</code> and <code>gvm</code>? One vendor manager at a time please.</p> <p>Glide will do it all. <code>glide init</code>, answer the questions, <code>glide up</code>.</p> <p>Off to the races.</p></pre>yowmamasita: <pre><p>Only used <code>gvm</code> to install go, it&#39;s not a package manager like <code>glide</code>. It&#39;s a version manager <a href="https://github.com/moovweb/gvm" rel="nofollow">https://github.com/moovweb/gvm</a></p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

856 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传