<p>I was thinking of putting my http handlers inside their own package and call it "handlers" but what if I wanted each handler to have their own package and still be within a "handlers" package such as "handlers/about-page"? 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 "net/http/cgi"?</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's it. Just Google'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 'cool_package'.</p>
<p>Then, create another directory called 'cool_package_number_two'. Make a file with the package name 'cool_package_number_two'.</p>
<p>Now, in order to use 'cool_package_number_two', you'll have to use the import path <code>$GOPATH/src/cool_package/cool_package_number_two</code></p>
<p>I'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 'src' 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 'p3':</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 "p1/p2/p3"
</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 "Multiple packages". Here is the quote:</p>
<p><em>"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."</em></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传