<p>I'm new to Go, coming from PHP. I've put together a basic blog CRUD app that uses MySql but it's all in one Go file and a bit of a mess. </p>
<p>I've completed the '<a href="https://golang.org/doc/articles/wiki/" rel="nofollow">Building a web app</a>' tutorial on the Go website which got me off to a good start with routing and templates, but it doesn't go into how you'd put together a full MVC application.</p>
<p>What are the best practices for structuring a Go web app? How do I separate out the model/controller layer? Should every controller be in it's own Go file? What's the best way of having a globally usable database connection?</p>
<p>For now, I'm just using the standard libraries - no frameworks.</p>
<p>As you can see, I have a lot of questions. Any advice or relevant articles would be greatly appreciated!</p>
<hr/>**评论:**<br/><br/>pragmaticprogrammer: <pre><p>Have a look at this <a href="https://github.com/josephspurrier/gowebapp" rel="nofollow">Github repo</a>. It a basic example for a full MVC application. It helped at least me to find a good starting point for my own projects.</p></pre>monkey_that: <pre><p>Take a look at this book and see if you see anything relevant
<a href="https://www.gitbook.com/book/astaxie/build-web-application-with-golang/details" rel="nofollow">https://www.gitbook.com/book/astaxie/build-web-application-with-golang/details</a></p>
<p>I think this was written by the author of Beego framework.</p></pre>ReachForTheSkyline: <pre><p>Nice, this looks ideal. Thanks!</p></pre>oefig: <pre><p>Keep it simple. Don't worry about "Design patterns" when writing Go.</p>
<p>Separate out all of your components into their own packages. Have a package for Users and a package for Posts and a package for Comments. Each package can have a file for the model, controller, etc. </p></pre>tex0: <pre><p>I hear this a lot (to not worry about design patterns). IMHO this is wrong. There are some very helpful patterns which help writing more idiomatic and concise go code.</p></pre>earthboundkid: <pre><p>I think people went overboard on patterns in the Java world, and there are still some negative feelings about that. But patterns per se aren't bad. You just need to know what fits the problem. MVC, for example, makes designing a web app much simpler. You can (and back in the day I have) make a web app without MVC, but it makes it easier to reason about. </p></pre>GreeTiger: <pre><p>Genuine question: are people building real production <em>web</em> apps in Go, and if so why?</p>
<p>It seems that inside and without the Go community there is an aversion to building Go webapps (whether following some variation on "MVC" or not) beyond very simple, stripped down microservices for a specific need. Whether that's due to Go's verbosity, lack of an ORM (or more accurately, lack of the language features you need to build a proper ORM), missing features in the core net/http libraries such as context handling, or whatever, there is a definite feeling that building a webapp in Go is a fool's errand and you're better off using e.g. Ruby or Python and keep Go for whatever heavy crunching you need to do in a backend service. </p>
<p>Does this reflect people's actual experience, or just prejudices?</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传