<p>Good morning,</p>
<p>I'm planning to write a forum backend (basically just a rest api) in Go. This will be my very first project in Go with which I want to learn the language. I just want it to have the basics (register, login, open thread, answer to thread).</p>
<p>Now I have to structure the whole project and I can't seem to find the optimal structure for such a project so is there any advice on how to do this?</p>
<p>I'm currently thinking about something like this:</p>
<pre><code>/config
config.go
/controllers
users.go
threads.go
posts.go
/http
server.go
/mysql
connection.go
database.go
queries.go
main.go
</code></pre>
<p>This way I could easily switch out the different packages and the basic application would still work.</p>
<p>Are there any recommendations/improvements?</p>
<hr/>**评论:**<br/><br/>peterbourgon: <pre><p>If this is your first project, don't waste energy on "optimizing" your package structure. There is no optimum layout. Do whatever allows you to progress to your goal quickly, and when you have something useful, revisit the structure.</p>
<p>edit: some things to look at, if you're really curious:</p>
<ul>
<li><a href="https://medium.com/@benbjohnson/standard-package-layout-7cdbc8391fc1#.sfxuencdz" rel="nofollow">https://medium.com/@benbjohnson/standard-package-layout-7cdbc8391fc1#.sfxuencdz</a></li>
<li><a href="https://github.com/thockin/go-build-template" rel="nofollow">https://github.com/thockin/go-build-template</a></li>
<li><a href="https://peter.bourgon.org/go-best-practices-2016/#repository-structure" rel="nofollow">https://peter.bourgon.org/go-best-practices-2016/#repository-structure</a></li>
</ul></pre>j_d_q: <pre><p>What he said + the suggestion of "for now, just put it all in the same package." Try not to use any private fields or functions outside of their general domain. Use interfaces if you have a good grasp on them - but don't feel averse to just use your structs (it took me a bit to realize how powerful and useful interfaces are in go).</p>
<p>My approach has been "break it out when breaking it out will prove useful." </p>
<p>When I do split packages, I put generic types at the root level. This would put user, thread and post structs+interfaces at /project. Then <code>{user,thread,post}_handler</code> in /project/http, and <code>{user,thread,post}_client</code> in /project/sql</p></pre>9hR4: <pre><p>The biggest problem I currently have is that I don't know where I should put which parts of the forum or how I should combine those parts.</p>
<p>I guess I will have to figure that out while progressing.</p>
<p>But thank you for the links, I will have a look at them. </p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传