Do you use multiple directories in GOPATH? Why?

xuanbao · · 618 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>According to <code>go help gopath</code>, &#34;The GOPATH environment variable lists places to look for Go code.&#34; In other words, you can specify multiple directories, not just <code>~/go</code> for example. </p> <p>I&#39;ve only ever used one directory and don&#39;t really understand why a person would want anything else.</p> <hr/>**评论:**<br/><br/>dgryski: <pre><p>I have two entries. One contains a the third party stuff I download (the first entry), the second is all my personal stuff.</p></pre>anacrolix: <pre><p>What if your own stuff gets into the first path?</p></pre>skelterjohn: <pre><p>How would that happen accidentally?</p></pre>countzero: <pre><p>For me, I don&#39;t organize my stuffs by language, but by applications, for example, store all AI stuffs in a place, financial stuffs in another, not matter they are in go, c++ or python. So I have several directories in GOPATH.</p></pre>0xjnml: <pre><p>Since GOPATH exists, it&#39;s set to $HOME on all my dev machines. I never understood the hate against that env var. Even worse, I consider getting rid of it a mistake.</p></pre>earthboundkid: <pre><p>My theory is that if GOPATH were split into GOSRC and GOPKG (there is already GOBIN), people wouldn&#39;t complain because it would just be completely normal, like wherever the hell other languages store their libraries. Having to have a src and pkg directory in GOPATH is just the thing that tips people over the edge into hating it.</p></pre>dc0d: <pre><p>Couldn&#39;t agree more!</p></pre>earthboundkid: <pre><p>No, but I will often do <code>GOBIN=$HOME/bin GOPATH=/tmp/whatever go get github.com/cool/new-project/cmd/cli</code> when I don&#39;t want to clutter my GOPATH with random dependencies.</p></pre>sh41: <pre><p>This was a while ago and my setup has changed slightly, but I answered this very question in detail in a <a href="https://dmitri.shuralyov.com/blog/18" rel="nofollow">How I use GOPATH with multiple workspaces</a> post.</p></pre>enocom: <pre><p>I read that just before posting. Sounds like your approach is the one others use as well.</p></pre>sh41: <pre><p>Yep.</p></pre>arp242: <pre><p>I have it set to <code>$HOME/go:$HOME/work:$HOME/code</code>; that way:</p> <ul> <li>Stuff I just <code>go get</code> to try out goes to <code>~/go</code>. This can be safely <code>rm -rf</code>&#39;d at any time.</li> <li>My work code goes in <code>~/work/</code> (I work from home and use the same machine for work and personal stuff).</li> <li>My personal stuff lives in <code>~/code</code>.</li> </ul> <p>The biggest caveat with this is that there are many scripts that use something like <code>cp -r foo $GOPATH/</code>, which won&#39;t work if you have multiple entries. Those scripts are broken and should be fixed. Luckily this is becoming less common now that <code>GOPATH</code> is no longer mandatory, but I had to fix quite a lot of scripts/projects when I started using Go a few years ago.</p> <p>I have a script for Go programs I want to keep installed (like <code>goimports</code> and whatnot) in <code>/usr/local/gotools</code>, so that&#39;s completely separate from my regular GOPATH:</p> <pre><code>#!/bin/sh export GOPATH=/usr/local/gotools repos=&#34; golang.org/x/tools/... github.com/nsf/gocode [..] &#34; for r in $repos; do echo &#34;$r&#34; go get -u &#34;$r&#34; done </code></pre> <p>As a sidenote, I hate GOPATH with a passion and can&#39;t wait for it to go away.</p></pre>Redundancy_: <pre><p>easyjson needs easyjson/gen to be importable when it runs. For that, it&#39;s potentially nice to install your tools to one GOPATH, then change your gopath for when you run easyjson to include both your repo and the tools code.</p></pre>tv64738: <pre><p>Here&#39;s a programmatic use of an extra entry in GOPATH: <a href="https://github.com/tv42/staged" rel="nofollow">https://github.com/tv42/staged</a></p></pre>

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

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