Go ignores gopath

agolangf · · 620 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;ve this in my .bashrc</p> <pre><code>GOPATH=$HOME/.go PATH=$PATH:$GOPATH/bin </code></pre> <p>echo $GOPATH returns the directory. However when I do <code>go get github.com/...</code> the installed packages appear in a directory $HOME/go (without dot).</p> <p>How can I fix that?</p> <hr/>**评论:**<br/><br/>tscs37: <pre><p><code>export GOPATH=$HOME/.go</code></p> <p>IIRC when you only do <code>GOPATH=$HOME/.go</code> bash will make this available in the <em>current</em> shell, while <code>export</code> makes it available to <em>child processes like go get</em>.</p></pre>I-just-came: <pre><p>Strange, <code>echo $GOPATH</code> returns <code>/Users/user/.go</code></p> <p>but <code>go env GOPATH</code> returns <code>/Users/user/go</code></p></pre>tscs37: <pre><p>it means that the shell is not propagating the variable into subprocesses so Go is using the default value.</p> <p>I do recommend to try <code>export</code></p></pre>I-just-came: <pre><p>Works with export. Thanks.</p></pre>epiris: <pre><p>A quick check to see if a variable is not being inherited by child processes is to run the same command proceeded by your var declaration, i.e.</p> <pre><code>$ GOPATH=~/.go go get -u ... </code></pre></pre>nhooyr: <pre><p><code>echo $GOPATH</code> first expands the variable <code>GOPATH</code> available in the current shell. So it becomes <code>echo /Users/user/.go</code>. <code>go env GOPATH</code> uses the <code>GOPATH</code> in the child process.</p></pre>TheMerovius: <pre><p>JFFR: Use &#34;env | grep GOPATH&#34;. As env is a sub-process (at least semantically), it won&#39;t know about local variables and only actual environment variables.</p></pre>shovelpost: <pre><p><code>$HOME/go</code> is the default GOPATH value (after Go 1.8 I believe) and I would highly recommend to use that.</p> <p>But if you really want to change it then use <code>export GOPATH=$HOME/.go</code> and make sure to close and open the terminal for the change to have an effect.</p> <p>Another thing that is very useful to add to your .bashrc or .profile is something like this:</p> <p><code>export CDPATH=.:$GOPATH/src:$GOPATH/src/github.com/&lt;your github username&gt;:</code></p> <p>Which will allow you to easily <code>cd</code> to your Go workspace from anywhere.</p></pre>I-just-came: <pre><blockquote> <p>$HOME/go is the default GOPATH value (after Go 1.8 I believe) and I would highly recommend to use that.</p> </blockquote> <p>Does it mean that I can safely remove lines </p> <p><code>GOPATH=$HOME/.go PATH=$PATH:$GOPATH/bin</code> from my bashrc? I want to use defaults.</p> <blockquote> <p>Which will allow you to easily cd to your Go workspace from anywhere.</p> </blockquote> <p>How so?</p></pre>shovelpost: <pre><p>I believe you still need the line:</p> <pre><code>export PATH=$PATH:$GOPATH/bin </code></pre> <blockquote> <p>Which will allow you to easily cd to your Go workspace from anywhere.</p> <p>How so?</p> </blockquote> <p>Setting this line in your .bashrc or .profile:</p> <pre><code>export CDPATH=.:$GOPATH/src:$GOPATH/src/github.com/&lt;your github username&gt;: </code></pre> <p>Makes your CDPATH include:</p> <ul> <li><code>.</code> </li> <li><code>$GOPATH/src</code></li> <li><code>$GOPATH/src/github.com/&lt;your github username&gt;</code></li> </ul> <p>So if we suppose you have <code>goproject</code> in your workspace then you can do <code>cd goproject</code> from any path and you will be transferred to <code>~/go/src/github.com/&lt;your github username&gt;/goproject</code>.</p> <p>Obviously you can modify CDPATH according to your needs.</p></pre>

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

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