Today I want to tell about my little experience with MySQL (although you can use any other database here) in Go.

The way to work with the database from Go

I found a great way to work with the database from Go and can't wait to share it.

In general, Go has several important things to do in order to make inquiries. For example, database/sql and github.com/go-sql-driver/mysql

A typical example looks like this:

GitHub Gist is loading...

And in order to map the query result into a structure, we will use github.com/jmoiron/sqlx.

SQLX is a set of extensions on top of the database/sql that allow you to make a minimal “ORM” in Go.

For example, we work with such a model

GitHub Gist is loading...

We have a simple database scheme

GitHub Gist is loading...

The connection to the database will look like this

GitHub Gist is loading...

SQLX bunch of methods

Sqlx has a bunch of methods, which start with a Must. MustExec, MustBegin. You have to use them with caution because they throw panic and your application can crash when you do not want it :)

CRUDs will look like this:

GitHub Gist is loading...

That's all. Just in case, the complete code:

GitHub Gist is loading...

Optimize your backend solutions with Mad Devs.

Latest articles here

Google analytics.

How We Set Up Google Analytics for Live Streaming App

In this article, I want to share my experience with video streaming for one of our Yourcast projects. You could find more about this project here:

 Top 3 best CI for your JS projects with Puppeteer tests.

Top 3 Best CI for Your JS Projects With Puppeteer Tests

For the first time, it was conceptualized and proposed by Grady Booch in 1991. It is one of the main elements of extreme programming practice.I...

How to Run Code Climate Code Analysis Locally Using VSCode + Remote-Containers.

How to Run Code Climate Code Analysis Locally Using VSCode + Remote-Containers

Everyone who writes code knows that the best code is code that is not written! But as they say, everyone is not perfect and we have to write code....

Go to blog