Local imports in Golang

xuanbao · · 493 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m noobie to Golang. Now I&#39;m working on my university project and it&#39;s becoming more complicated so I wanted to split code between several files. 1) Is there a way to create directory with some modules like in python: import &#34;./dir/some_module&#34; 2) Is there any programs that creates virtualenv like in python.</p> <hr/>**评论:**<br/><br/>programfog: <pre><p><a href="http://stackoverflow.com/a/15051192/275583" rel="nofollow">http://stackoverflow.com/a/15051192/275583</a></p></pre>peterbourgon: <pre><p>To clarify, the first answer to that question is correct, i.e. you should (must) always import the fully-qualified path of a package, e.g. <code>github.com/yourname/yourrepo/mypkg</code>. </p> <p>The second answer is incorrect, i.e. you shouldn&#39;t (can&#39;t) import in a relative sense, e.g. <code>./mypkg</code>. </p></pre>luminoumen: <pre><p>but if, for example, project will move from github to bitbucket I need to fix all imports?</p></pre>PsyWolf: <pre><p>Yes, but with Go the whole process is as easy as </p> <p><code>gofmt -w -r &#39;&#34;github.com/foo/bar&#34; -&gt; &#34;bitbucket.com/foo/bar&#34;&#39; *.go</code></p> <p>(Note that the whole argument to -r is in single quotes, with double quotes inside it.) </p> <p>From <a href="https://groups.google.com/forum/m/#!topic/golang-nuts/RyhFfhB_kd8" rel="nofollow">this golang-nuts thread</a></p></pre>PsyWolf: <pre><p>Alternatively, if you really need a level of indirection, check out <a href="https://texlution.com/post/golang-canonical-import-paths" rel="nofollow">vanity and canonical import paths</a>. It&#39;s overkill for most people, but if you actually expect to change repo hosting, that&#39;s how the go team managed their move away from code.google.com</p></pre>luminoumen: <pre><p>Imagine I changed github-&gt;bitbucket. So, I can&#39;t event build my project before I commit my changes to bitbucket? How you live with this, people?</p></pre>KenjiTakahashi: <pre><blockquote> <p>Imagine I changed github-&gt;bitbucket. So, I can&#39;t event build my project before I commit my changes to bitbucket?</p> </blockquote> <p>You don&#39;t have to commit anything to do the build. Just move the project to proper path(s) in your $GOPATH.</p> <blockquote> <p>How you live with this, people?</p> </blockquote> <p>We don&#39;t throw our projects around.</p></pre>joushou: <pre><p>You should instead be whining about &#34;What if I fork a popular project on github, and it won&#39;t build because the import paths include the username?&#34;</p> <p>It sucks, yeah, but <em>other than that</em>, the system actually works kind of well.</p></pre>peterbourgon: <pre><p>Yes. (Or use a vanity import path, if you own a domain.)</p> <p>In practice, I think this has happened approximately never.</p></pre>KenjiTakahashi: <pre><p>I&#39;ve seen it happen once when googlecode shat down.</p></pre>TheMerovius: <pre><p>If your project moves from github to bitbucket, it becomes a different package (as packages are identified by their import paths). This is a feature, not a bug. If you don&#39;t want a different entity to own your package name (because that&#39;s what&#39;s effectively the case, when your package name begins with <code>github.com</code> or <code>bitbucket.org</code>), use a vanity import path.</p></pre>k_u_r_o_k_u_s_e: <pre><blockquote> <p>If your project moves from github to bitbucket, it becomes a different package</p> </blockquote> <p>this. How is it so hard to understand. go &#34;packages as URI&#34; have a few problems , but this is certainly not an issue. </p> <p>1 URI = 1 package , end of story.</p></pre>luminoumen: <pre><p>This answer didn&#39;t work for me. Can you please clarify it for me. If I have 3 lib files and one main. Then I move 3 lib files to directory named lib/ then how to make it work?</p></pre>k_u_r_o_k_u_s_e: <pre><p>If you move 3 packages into a different directory then they are different packages, not the same.</p></pre>luminoumen: <pre><p>My repo <a href="https://github.com/luminousmen/Master" rel="nofollow">https://github.com/luminousmen/Master</a></p></pre>

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

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