Package within a package

agolangf · · 638 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I was thinking of putting my http handlers inside their own package and call it &#34;handlers&#34; but what if I wanted each handler to have their own package and still be within a &#34;handlers&#34; package such as &#34;handlers/about-page&#34;? How do you structure the directories and build that?</p> <hr/>**评论:**<br/><br/>slantview: <pre><p>Every package is independent. There is no inheritance in Go.</p></pre>goboy234: <pre><p>If so, how do they do &#34;net/http/cgi&#34;?</p></pre>slantview: <pre><p>That is just the layout of the directories. Those packages are only related by location.</p></pre>goboy234: <pre><p>Yep. That&#39;s it. Just Google&#39;d the right question and got the same answer. Thanks.</p></pre>barsonme: <pre><p>Simple.</p> <p>Run <code>mkdir -p $GOPATH/src/cool_package</code> and then <code>cd</code> into that directory. Then, create a file with the package name &#39;cool_package&#39;.</p> <p>Then, create another directory called &#39;cool_package_number_two&#39;. Make a file with the package name &#39;cool_package_number_two&#39;.</p> <p>Now, in order to use &#39;cool_package_number_two&#39;, you&#39;ll have to use the import path <code>$GOPATH/src/cool_package/cool_package_number_two</code></p> <p>I&#39;m on my phone, I could describe it better were I on my laptop.</p></pre>jbuberel: <pre><p>Rember that by convention (not rule), the name of a package should be the same as the name of the directory that contains it. The full import path is composed by the directory structure relative to the top-level &#39;src&#39; directory that contains it.</p> <p>So if you had foo.go inside this tree:</p> <pre><code>gopath/ `-- src `-- p1 `-- p2 `-- p3 `-- foo.go </code></pre> <p>The file foo.go should declare itself part of package &#39;p3&#39;:</p> <pre><code>package p3 </code></pre> <p>Any file that wanted to use a symbol exported in that file would use the following import path:</p> <pre><code>import &#34;p1/p2/p3&#34; </code></pre> <p>In that client source file, by default you would refer to all symbols using:</p> <pre><code>p3.DoStuff() </code></pre></pre>maedo: <pre><p>I was having similar Go packaging questions, when I was directed to the very helpful article, <a href="http://dave.cheney.net/2014/12/01/five-suggestions-for-setting-up-a-go-project" rel="nofollow">http://dave.cheney.net/2014/12/01/five-suggestions-for-setting-up-a-go-project</a>. All my packaging problems are answered there, and I believe including yours as well. Ref, the section &#34;Multiple packages&#34;. Here is the quote:</p> <p><em>&#34;I’ve chosen my own term project, <a href="https://github.com/pkg/term" rel="nofollow">https://github.com/pkg/term</a>, which contains two pacakges, github.com/pkg/term, and github.com/pkg/term/termios, ... Even though Go does not have a notion of sub packages, term and term/termios live in the same repository. I could have created two projects, <a href="https://github.com/pkg/term" rel="nofollow">https://github.com/pkg/term</a> and <a href="https://github.com/pkg/termios" rel="nofollow">https://github.com/pkg/termios</a>, but as they are closely related, it made sense to place the source for both packages in the same Github repository.&#34;</em></p></pre>

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

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