TASK 1: Insert Multiple Rows into a table

Inserting numerous rows can be error-prone. With dbq , it is as thoughtless as inserting a single row. You can even pass data directly as []string or []int instead of having to convert to []interface{} annoyingly.

Inserting multiple rows

In the example above, the standard code seems more straightforward. But in reality, it is less readable — especially as the number of rows increases. You can also work directly with structured data.

TASK 2: Fetch and Unmarshal all rows from a table

This is where dbq shines. Unmarshaling happens magically via the popular mapstructure package.

Fetching all rows

I think the contrast speaks for itself. The results look something like this for both queries:

([]*main.Row) (len=6 cap=8) {

(*main.Row)(0xc00009e1c0)({

ID: (int) 1,

Name: (string) (len=5) "Sally",

Age: (int) 12,

CreatedAt: (time.Time) 2019-03-01 00:00:00 +0000 UTC

}),

(*main.Row)(0xc00009e300)({

ID: (int) 2,

Name: (string) (len=5) "Peter",

Age: (int) 15,

CreatedAt: (time.Time) 2019-02-01 00:00:00 +0000 UTC

})

}

Other Features

Flatten Query Args

All slices are flattened automatically.

Single Result Fetches

Some queries are going to at most return 1 row. In that case, you can pass the SingleResult option. This will make the Q and MustQ function return a single row (instead of a slice) or nil .

MySQL context cancelation

As you probably already know, context cancelation doesn’t actually cancel the underlying MySQL query. dbq plays nicely with the mysql-go package.

Conclusion

You can see that when you are prototyping and/or trying to get things done, dbq really improves readability and convenience.

You can find the repository here: https://github.com/rocketlaunchr/dbq

Stars and PR’s are always appreciated.