Dealing with SQL Queries in GoLang

On this post I will add my notes related to the handling of SQL Queries in GoLang. Especially complex queries. Its work in progress so I will keep adding my notes here.

A bit of background: Recently I wrote few straightforward micro-services e.g. user management, notifications, comments, etc. That actually requires to interact with the Postgres database for CRUD operations.

So far its good, but when you really dive into the code then you see raw sql queries. It looks okay for time being , but as soon the requirements extends it looks like not fair for a developer to write a bit ugly and repeated code e.g.

  • Query to get a user by Id
  • Query to get a user by email
  • Query to get a user by zip-code
  • Query to get users by created date
  • Query to get users by password expire date

Refer : https://studygolang.com/resources/14744

To solve the above ,

The first step is to look around GitHub and see if you can find any ORM similar to Gorrila Mux in routing. If you have a bit python Django or Java background then temptation goes towards a similar like Django or java based.

I have already done the first step and many more, but still not convinced what has been offered so far from Github.

Many repos offers SQL Query Builder related stuff, that I am still analysing. As I am working on a serious project, so possibly I will write my own sql query builder, and mapping results from query to models.

In between found this post to read:

https://andrewpillar.com/programming/2019/07/13/orms-and-query-building-in-go/

References :

Jet is the easiest and the fastest way to write complex type-safe SQL queries as a Go code and map database query result into complex object composition. It is not an ORM.

https://github.com/go-jet/jet

https://github.com/andrewpillar/query/blob/master/query.go

https://github.com/jmoiron/sqlx

https://github.com/gchaincl/dotsql

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s