<p>Hi,
I'm considering using Golang for my main language for web development, I'm trying to see if there is anything resembling Python'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'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 "language standards" 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 "orm" style syntax. Knowing SQL isn't a bad thing and it is easy to port.</p>
<p>I worked with ORM's in Ruby/Python land for a long time. It doesn't seem helpful with Go though. </p>
<p>Also if you're using SQL there is no more... "how do I do this query in this ORM?!"</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's ORM sort of does that and includes a migration system that isn'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't want that, you might be able to use the ORM on it's own (it's called "pop").</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'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'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 "framework"; this is a very much flame-provoking topic as—on the one hand—there is a strong "start small" 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'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'm in the first camp (and <a href="https://www.youtube.com/watch?v=yi5A3cK1LNA" rel="nofollow">here'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's a working one</a>; I'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 "framework vs library" 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'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("name = ?", "jinzhu").First(&user)</code>, which is a bit problematic when you try do to a lot of dynamic queries in some cases. I'm looking for something like (pseudocode):</p>
<p><code>query.Where(User.name_col == "jinzhu")</code> etc.</p>
<p>Something like this is particularly problematic in many cases <code>db.Where("name = ? AND age >= ?", "jinzhu", "22").Find(&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's - eg <code>DB.Where(...).Where(...).Find(...)</code></p>
<p>You can also query with types: <code>db.Where(&User{Name: "jinzhu", 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'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 == "jinzhu")
</code></pre>
<p>Not gonna work with go, because we can't override operators</p></pre>ergo14: <pre><p>Yeah, I don't expect go to be python so maybe something like:</p>
<p><code>query.Where(ops.eq(User.col, "jinzhu"))</code> would work? For now i'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's tackle this nicely.</p></pre>kaeshiwaza: <pre><p>If you know Pyramid you will like Go. "pay only for what you eat"</p>
<p>Pyramid helped me a lot to understand how to make my own framework in Go. I mean you don'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't see that is so much drivers ?</p></pre>ergo14: <pre><p>Well pyramid still has quite a bit of functionality, I'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't see in Go stdlib ?</p></pre>ergo14: <pre><p>Well, extensibility hooks, event system, acl system, etc. I haven't tried golang really so I can'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'd like to rewrite in Go. Don't know exactly how I will do it but It'll be a fun challenge, and for sure no framework exist like that !</p></pre>ergo14: <pre><p>I'm not sure you can have something like zodb in go? I'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传