Considering adopting golang as main language for web

xuanbao · · 458 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi, I&#39;m considering using Golang for my main language for web development, I&#39;m trying to see if there is anything resembling Python&#39;s SQLAlchemy - basicly elliminating text query stitching (and to be clear I know SQL itself).</p> <p>And a web framework that would resemble Pyramid web framework functionally wise.</p> <p>I&#39;m also trying to wrap my head around the fact that there are tons of db drivers, when I looked last time 2 years ago I was completely unsure which one would be the right to use - are there some &#34;language standards&#34; that are most used and supported well by community?</p> <hr/>**评论:**<br/><br/>lhxtx: <pre><p>I’ve used both python and go for dB heavy tasks. I spend less time in golang writing raw sql queries and using go’s fantastic sqlx package to make it easy to populate structs than I did messing with python classes and sqlalchemy. </p> <p>Also, remember you’ll need a lot less server to get similar performance so a small amount of extra time spent writing performant go may be worth it. </p></pre>scottjbarr: <pre><p>sqlx is the business. No &#34;orm&#34; style syntax. Knowing SQL isn&#39;t a bad thing and it is easy to port.</p> <p>I worked with ORM&#39;s in Ruby/Python land for a long time. It doesn&#39;t seem helpful with Go though. </p> <p>Also if you&#39;re using SQL there is no more... &#34;how do I do this query in this ORM?!&#34;</p> <p>(edit: spelling, as always)</p></pre>FierceDeity_: <pre><p>I am kinda considering adopting a query generator that outputs the SQL to me if it can actually abstract the differences between postgre, mysql and sqlite away. I have my reservations if it can do so successfully, but if it can, it would reduce the amount of database queries to write.</p></pre>lhxtx: <pre><p>Probably easier to abstract your database layer as an interface, and then write your actual sql for each dB if you need to support multiple dbs. Try to keep your business logic “above” the dB interface so you’re only writing the bare minimum database methods for each dB. </p> <p>Why are you so worried about writing some sql? It’s just not that hard...</p></pre>robvdl: <pre><p>Gobuffalo&#39;s ORM sort of does that and includes a migration system that isn&#39;t based on pure SQL migration files, but these more generic .fizz files. Most migrations frameworks I have seen in Go use pure SQL migrations which ties things to a specific DB system. But Buffalo is also a whole web framework that makes a lot of choices for you like taking care of the frontend asset pipeline. If you don&#39;t want that, you might be able to use the ORM on it&#39;s own (it&#39;s called &#34;pop&#34;).</p></pre>robvdl: <pre><p>It always seems to amaze me that people downvote informative posts :) Not that I care, just sayin.</p></pre>FierceDeity_: <pre><p>Yeah I didn&#39;t do it</p></pre>shovelpost: <pre><p>Speaking about query builders, <a href="https://github.com/ulule/loukoum" rel="nofollow">this</a> one looks pretty sweet. Pun intended.</p></pre>lhxtx: <pre><p>Yup sqlx Get() and Select() are basically magic. :)</p></pre>kostix: <pre><p>I&#39;d recommend to start small and use <a href="https://godoc.org/github.com/jmoiron/sqlx" rel="nofollow"><code>github.com/jmoiron/sqlx</code></a>.</p> <p>As to &#34;framework&#34;; this is a very much flame-provoking topic as—on the one hand—there is a strong &#34;start small&#34; movement in the Go community suggesting to bootstrap your project using nothing but Go and its standard library and only throw more 3rd-party stuff in once you&#39;re confident you <em>need</em> it, and—on the second hand—Go has nothing like RoR, so everyone has their own pet web framework.</p> <p>I&#39;m in the first camp (and <a href="https://www.youtube.com/watch?v=yi5A3cK1LNA" rel="nofollow">here&#39;s why</a>) but if you insist on dealing with a framework the two <em>universal</em> recommendations are:</p> <ul> <li><p><a href="http://www.florinpatan.ro/2016/10/why-you-should-not-use-iris-for-your-go.html" rel="nofollow">Stay clear of Iris</a>.</p></li> <li><p><a href="http://web.archive.org/web/20140625195045/https://codegangsta.io/blog/2014/05/19/my-thoughts-on-martini/" rel="nofollow">Stay clear of frameworks which do magic</a> — Go itself is entirely antithetical to magic. (The post I cited refers to another one; the link is broken, so <a href="https://stephensearles.com/three-reasons-you-should-not-use-martini/" rel="nofollow">here&#39;s a working one</a>; I&#39;d also recommend to read <a href="https://stephensearles.com/framework-vs-library/" rel="nofollow">this</a> by the same author which nicely dissects the &#34;framework vs library&#34; situation).</p></li> </ul></pre>melevine45: <pre><p><a href="http://www.gorm.io" rel="nofollow">www.gorm.io</a> may be useful.</p></pre>robvdl: <pre><p>I really don&#39;t get why people like Gorm so much, it does soft deletes by default if you embed their Model struct, adding a deleted_at column to everything, which is horrible for querying and leaves data in your database. What I did when I was using Gorm is I created my own base model which only had the created_at field but not the deleted_at field, that turns of soft deletes.</p></pre>ergo14: <pre><p>Yeah it looks nice, but it still has string stitching <code>db.Where(&#34;name = ?&#34;, &#34;jinzhu&#34;).First(&amp;user)</code>, which is a bit problematic when you try do to a lot of dynamic queries in some cases. I&#39;m looking for something like (pseudocode):</p> <p><code>query.Where(User.name_col == &#34;jinzhu&#34;)</code> etc.</p> <p>Something like this is particularly problematic in many cases <code>db.Where(&#34;name = ? AND age &gt;= ?&#34;, &#34;jinzhu&#34;, &#34;22&#34;).Find(&amp;users)</code> when you do dynamic search on multiple parameters that can be different types - think, shop filter functionality for search.</p></pre>joncalhoun: <pre><p>You can chain the where&#39;s - eg <code>DB.Where(...).Where(...).Find(...)</code></p> <p>You can also query with types: <code>db.Where(&amp;User{Name: &#34;jinzhu&#34;, Age: 20})</code></p></pre>ergo14: <pre><p>Awesome, can you do things like: <code>DB.Where(DB.And(clause1, DB.Or(clause2, clause3)))</code> ?</p></pre>joncalhoun: <pre><p>I would suggest experimenting with it a bit. See <a href="http://gorm.io/docs/query.html" rel="nofollow">http://gorm.io/docs/query.html</a> for some examples to start with, and as you branch out enable log mode (<a href="https://godoc.org/github.com/jinzhu/gorm#DB.LogMode" rel="nofollow">https://godoc.org/github.com/jinzhu/gorm#DB.LogMode</a>) so you can see the actual SQL queries being used.</p> <p>You can do AND and OR clauses, but they aren&#39;t quite the way you described. That said, you could wrap GORM to make them happen.</p></pre>FierceDeity_: <pre><pre><code>query.Where(User.name_col == &#34;jinzhu&#34;) </code></pre> <p>Not gonna work with go, because we can&#39;t override operators</p></pre>ergo14: <pre><p>Yeah, I don&#39;t expect go to be python so maybe something like:</p> <p><code>query.Where(ops.eq(User.col, &#34;jinzhu&#34;))</code> would work? For now i&#39;m just thinking in pseudocode.</p></pre>FierceDeity_: <pre><p>Of course, that would work if the project implements it like that</p></pre>ergo14: <pre><p>Well that seems to be one of more reasonable ways to create programmatic interfaces to sql that would be also cross database/driver that I can think of. I wonder if there are some ways golang orm&#39;s tackle this nicely.</p></pre>kaeshiwaza: <pre><p>If you know Pyramid you will like Go. &#34;pay only for what you eat&#34;</p> <p>Pyramid helped me a lot to understand how to make my own framework in Go. I mean you don&#39;t need any external framework. You just add slowly what you need. Often you will do your middleware yourself. You can even do your own router like traversal if you want.</p> <p>For database, I use lib/pq and Postgresql. I don&#39;t see that is so much drivers ?</p></pre>ergo14: <pre><p>Well pyramid still has quite a bit of functionality, I&#39;d ideally try to avoid reinventing the wheel :)</p></pre>kaeshiwaza: <pre><p>We have routing, template calling, interface for middleware, assets serving, request and response, logging. And even more with websocket for example.</p> <p>The benefit of working with Pyramid is that you know exactly what you need and where it will be :) for example authentification will go in a middleware.</p> <p>What do you miss from Pyramid that you don&#39;t see in Go stdlib ?</p></pre>ergo14: <pre><p>Well, extensibility hooks, event system, acl system, etc. I haven&#39;t tried golang really so I can&#39;t give a specific answer, the question was general in its nature. I only have more concrete questions about go ORM solutions because they have examples.</p></pre>kaeshiwaza: <pre><p>I found hooks and event system easy to implement in Go.</p> <p>acl system is the only things I found not easy to do. I mean to do the right way and reuse.</p> <p>I did a cms with traversal with Pyramid and zodb that I&#39;d like to rewrite in Go. Don&#39;t know exactly how I will do it but It&#39;ll be a fun challenge, and for sure no framework exist like that !</p></pre>ergo14: <pre><p>I&#39;m not sure you can have something like zodb in go? I&#39;m postgresql fan myself though.</p></pre>LordDizzy: <pre><p>Check out <a href="https://gobuffalo.io" rel="nofollow">https://gobuffalo.io</a>.</p></pre>

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

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