Package name redirecting

agolangf · · 434 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>In the file <code>github.com/upspin/cmd/upspinserver/main.go</code>, the code imports the package <code>upspin.io/cloud/storage/disk</code>, but a visit to that URL shows a 404 (not found). The code for that package is actually located at <code>github.com/upspin/cloud/storage/disk</code>:</p> <pre><code>// Copyright 2016 The Upspin Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Command upspinserver is a combined DirServer and StoreServer for use on // stand-alone machines. It provides only the production implementations of the // dir and store servers (dir/server and store/server). package main // import &#34;upspin.io/cmd/upspinserver&#34; import ( &#34;upspin.io/cloud/https&#34; &#34;upspin.io/serverutil/upspinserver&#34; // Storage implementation. _ &#34;upspin.io/cloud/storage/disk&#34; ) func main() { ready := upspinserver.Main() https.ListenAndServeFromFlags(ready) } </code></pre> <p>How is this working with the Go compiler? Is the server at upspin.io just doing a redirect?</p> <p>Can I use this same trick in my own code? In other words, if my code lives at <code>github.com/someuser/foo</code>, can I import it as <code>mycompany.com/myproject/foo</code>?</p> <hr/>**评论:**<br/><br/>sh41: <pre><p>Yes, those are so-called vanity import paths, and you can create your own. It is documented in the second half of <a href="https://golang.org/cmd/go/#hdr-Remote_import_paths">https://golang.org/cmd/go/#hdr-Remote_import_paths</a> section.</p> <p>Also, it’s worth mentioning that the Go compiler doesn’t deal with this, the <code>go get</code> tool does. The Go compiler just finds Go packages on disk, in your GOPATH workspace where <code>go get</code> has placed them.</p></pre>grkuntzmd: <pre><p>So in the example above, <code>go get</code> would place the file in <code>$GOPATH/src/upspin.io/cloud/storage/disk</code>?</p></pre>sh41: <pre><p>Precisely.</p> <p>You can try it. Do:</p> <pre><code>export GOPATH=/tmp/empty go get -v upspin.io/cmd/upspinserver tree $GOPATH/src </code></pre> <p><code>go get</code> will fetch all the necessary dependencies of <code>upspinserver</code>, including <code>upspin.io/cloud/storage/disk</code>.</p> <p>You can also view the source code of that package at its canonical import path via gotools.org. See <a href="https://gotools.org/upspin.io/cmd/upspinserver" rel="nofollow">here</a>.</p></pre>shovelpost: <pre><p><a href="https://texlution.com/post/golang-canonical-import-paths/" rel="nofollow">Yes.</a></p> <p>Also read <a href="https://golang.org/cmd/go/#hdr-Remote_import_paths" rel="nofollow">this</a>.</p></pre>grkuntzmd: <pre><p>I did not know about the <code>import &#34;path&#34;</code> comment. Thanks.</p></pre>TheMerovius: <pre><p>Because this hasn&#39;t been explicitly mentioned:</p> <p><a href="https://upspin.io/cloud/storage/disk?go-get=1" rel="nofollow">https://upspin.io/cloud/storage/disk?go-get=1</a></p></pre>dabshores: <pre><p>As a long time go dev, my advice is to ignore the import path and trust <code>git ls-remote --get-url</code> and <code>git rev-parse HEAD</code></p></pre>

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

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