Articles or advice on design patterns for a web app?

polaris · · 566 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m new to Go, coming from PHP. I&#39;ve put together a basic blog CRUD app that uses MySql but it&#39;s all in one Go file and a bit of a mess. </p> <p>I&#39;ve completed the &#39;<a href="https://golang.org/doc/articles/wiki/" rel="nofollow">Building a web app</a>&#39; tutorial on the Go website which got me off to a good start with routing and templates, but it doesn&#39;t go into how you&#39;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&#39;s own Go file? What&#39;s the best way of having a globally usable database connection?</p> <p>For now, I&#39;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&#39;t worry about &#34;Design patterns&#34; 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&#39;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 &#34;MVC&#34; or not) beyond very simple, stripped down microservices for a specific need. Whether that&#39;s due to Go&#39;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&#39;s errand and you&#39;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&#39;s actual experience, or just prejudices?</p></pre>

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

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