News for hackers. Stuff that matters.
Re:A RESTful Micro-Framework in Go by tptacek on Sunday January 26, 2014 @12:13PM (#7125780)
This looks like great work, so probably this comment is a little off topic, but here goes anyways:

Every web framework I've seen for Golang seems to do a competent job of tackling the easiest part of designing a web framework, which is routing requests to handlers.

Actually, the built-in net/http library already does a serviceable job of handling requests, and if you understand closures, filtering them and maintaining request state as well.

The thing no framework I have seen so far addresses is persistence. SQL persistence in Golang is a bit of a nightmare. It's raw SQL, and the database libraries are fussy.

The thing that makes Rails so easy to write in, especially for newcomers, is ActiveRecord. As far as I can tell, nothing like it exists (in a stable form) for Golang.

Re:A RESTful Micro-Framework in Go by carbocation on Sunday January 26, 2014 @12:40PM (#7125922)
I agree with you re: the lack of ORMs in golang. I actually enjoy writing and profiling my own SQL, but if I want to prototype, ORMs can speed up the process.

I will say that some of the parts of SQL in golang that I find most annoying, such as populating structs, are handled very nicely by jmoiron's sqlx library. [1] This is not an ORM, but it's an extension to sql that removes some of the most annoying boilerplate nevertheless.

1 = https://github.com/jmoiron/sqlx

Re:A RESTful Micro-Framework in Go by jamra on Sunday January 26, 2014 @01:44PM (#7126221)
This looks like an awesome library. I like how he does this:

type Person struct { FirstName string `db:"first_name"` LastName string `db:"last_name"` Email string }

to map columns to structs.

Re:A RESTful Micro-Framework in Go by gtaylor on Sunday January 26, 2014 @12:56PM (#7125978)
I hadn't seen sqlx before, but this looks super convenient. Thanks for the tip!
Re:A RESTful Micro-Framework in Go by mbreese on Sunday January 26, 2014 @12:38PM (#7125908)
I find the lack of ORM a feature in a web framework. Personally, I find them frustrating and an extra layer of abstraction that just gets in my way. Then again, I also don't mind writing queries. I usually end up using my own lightweight set of helper functions to manage queries, regardless of the language.

I still have Hibernate flashbacks from back in the day.

Re:A RESTful Micro-Framework in Go by masklinn on Sunday January 26, 2014 @03:09PM (#7126608)
> I find the lack of ORM a feature in a web framework.

There's quite a lot of stuff between "write raw SQL queries" and "full-blown ORM". http://docs.sqlalchemy.org/en/latest/core/tutorial.html for instance.

Re:A RESTful Micro-Framework in Go by boomlinde on Monday January 27, 2014 @05:50AM (#7129698)
I don't think that the argument is that ORMs are bad, but that it shouldn't necessarily fall into the scope of a web framework. I think you could reasonably argue that the primary task of a web framework is to be a meaningful abstraction layer over http requests and http responses, but on the other hand I can also see why someone would disagree. In some sense you could make the same argument against template rendering systems.

Personally, I work a lot with a database setup that is managed primarily through REST and JSON-RPC interfaces, which makes a framework that lacks ORM a perfect fit, and I'd rather use something like Flask than something that sets me up with an ORM library whether I need it or not.

Re:A RESTful Micro-Framework in Go by sdegutis on Sunday January 26, 2014 @01:01PM (#7126002)
ActiveRecord may be a stepping stone for those just starting out, but for serious, long-living, evolving applications I firmly believe it does more harm than good.

Back when I did Rails contracting, cleaning up code which conflated database access with business logic was quite possibly what was keeping us in business.

Re:A RESTful Micro-Framework in Go by bliti on Sunday January 26, 2014 @12:45PM (#7125938)
Yes, SQL is still spotty in Go. I wrote a medium sized program with it and the learning curve was fairly high. Documentation for the database libraries is mostly nill. Though I'm not sure if it would benefit from an ORM. This is a systems language. I'd rather have it use interfaces to access data from a database than have it deal with it itself. That way I keep it tidy and fast. Which is Go's aim.
Re:A RESTful Micro-Framework in Go by icholy on Sunday January 26, 2014 @12:50PM (#7125956)
I find the docs very comprehensive. http://golang.org/pkg/database/sql/
Re:A RESTful Micro-Framework in Go by bliti on Sunday January 26, 2014 @07:29PM (#7127899)
I failed to convey my experiences. Which is that the third party drivers are very hit and miss (along with the documentation). Understandably due to how young the language is. But yes, the golang docs are very nice.
Re:A RESTful Micro-Framework in Go by iv_08 on Sunday January 26, 2014 @01:03PM (#7126012)
There's even a nice tutorial: http://go-database-sql.org/overview.html
Re:A RESTful Micro-Framework in Go by JonnieCache on Sunday January 26, 2014 @08:56PM (#7128220)
If anyone is planning to write a golang ORM, you should have a look at sequel: http://sequel.jeremyevans.net/

I'm not aware of a better ORM anywhere. The simplicity, and the way it separates the model classes from everything else, is sublime. It does pretty much everything activerecord does and lots more, without all the layers of abstraction.

Re:A RESTful Micro-Framework in Go by chimeracoder on Sunday January 26, 2014 @12:31PM (#7125872)
I'm going to chime in, since I've been using Go as my primary language for over a year, and I'm currently working on a major (production) Go project that others might see as well-suited for Rails (in fact, the early prototype was written in Rails[0])

> The thing no framework I have seen so far addresses is persistence. SQL persistence in Golang is a bit of a nightmare. It's raw SQL, and the database libraries are fussy.

> The thing that makes Rails so easy to write in, especially for newcomers, is ActiveRecord. As far as I can tell, nothing like it exists (in a stable form) for Golang.

I wouldn't recommend newcomers to Go use SQL right off the bat. Document-based storage (like CouchDB or MongoDB), or even key/value (Redis, etc.) is much more aligned with Go's concept of structs, and all three of those databases have strong library support in Go. If you're just looking to dabble in Go and just happen to need a database (and it doesn't matter which), I'd recommend playing around with those first. Even Neo4j works very well with Go, though as of January 2013 (when I last used it), the only Go library for Neo4j was third-party, and not very well-documented.

That said, once you know the Go language, using SQL (either with or without an ORM) is straightforward, if not as convenient as ActiveRecord. jmoiron has a good blog post[1] comparing the good, bad, and ugly of Go ORMs, so I won't try to top that here - rather, I'll share my experience.

(As pointed out elsewhere, ORMs are not universally liked as a general concept, so I'm writing this comparison assuming that one actually does want the features an ORM provides - naturally, if you prefer prepared SQL, Go already provides perfect support for that.)

Gorp is definitely not as "magical" as ActiveRecord, but it certainly does the job. A number of the ways in which it differs from ActiveRecord have more to do with the (strongly) static vs. dynamic difference in type systems, though some are specific to Go's architecture.

There are a few odd features here and there that are still missing (for example, I'm actually not sure if there's a wrapper function for "INSERT IF NOT EXISTS" yet, though it's not a huge bother).

I do end up spending more time thinking about the queries that I'm using, compared to Django or Rails. There is some additional verbosity - I have to write some helper functions that I wouldn't otherwise have to. On the other hand, I know exactly what queries are being executed at all times, so it's easy to identify potential bottlenecks.

In short - I agree that ActiveRecord is more convenient than anything that currently exists in Go. On the other hand, it's reasonably advanced enough that relative newcomers to the language[2] shouldn't have a problem with it. So far, the biggest bottleneck in development of this application has not been with the SQL bindings, even though it would be nice to spend a bit less time on that front.

It hasn't had as much time to develop and mature as ActiveRecord have, but those of us using it in production have accepted those tradeoffs.

[0] If you're wondering why we didn't continue it in Rails, we had to scrap the original codebase for a number of other reasons, so given that we weren't keeping any of the original code, there was no logical reason we needed to use the same language.

[1] http://jmoiron.net/blog/golang-orms/

[2] As opposed to newcomers to programming in general

Re:A RESTful Micro-Framework in Go by jamra on Sunday January 26, 2014 @12:40PM (#7125925)
What's wrong with using raw sql classes? A web framework should be database agnostic.

I agree with the usefulness of a better SQL library though.

Edit: Wow that was worded poorly. What I meant was, what do you find bad about the SQL library?

Re:A RESTful Micro-Framework in Go by shadowmint on Sunday January 26, 2014 @07:23PM (#7127873)
How about having two statements for running queries, Exec, and Query, but unlike other database engines where you might expect to be able to execute a PQ query that returns values like:

    INSERT INTO PERSON (NAME) VALUES ('JACK') RETURNING ID_PERSON, CREATION_TIME
...no, actually, you cannot perform a query for an INSERT statement, even if it returns values.

This isn't a database driver issue even, apparently. It's just the way the database API was defined. Query is not for inserts.

Or the fact that if you run a query (eg. CREATE TABLE blah...) you get a result set that you have to call Next() on to flush the query to the database, even if you close the connection.

...because obviously, running next on a create statement is what you would do.

Or how you have to import a singleton to load the database driver you want.

    import (
	_ "github.com/lib/pq"
	"database/sql" <----- Previous import has side effects.
    )
How is that idiomatic go? -__- It's totally a hack.

It's nothing major, but the database/sql library api just doesn't feel right.

It's messy.

Re:A RESTful Micro-Framework in Go by icholy on Sunday January 26, 2014 @12:35PM (#7125892)
IMO having some ORM automagically construct queries for you is a pretty scary idea. Obviously it's not a good idea to have SQL scattered all over your codebase (we've all seen it). But including SQL in your code isn't hard if you keep it in one place and abstract it properly.