Databases
Latest update: March 2, 2023

Databases #

Like dependency injection, talking to databases is a topic the Go community likes to be particularly opinionated about. Coming from Java, Ruby on Rails, or any sort of well-known framework you will certainly look for the cushioned comfort of Hibernate, or your favorite Object Relational Mapper (ORM) of choice.

Which is I am starting this section with a word of caution, because the term ORM is quite a loaded one among the community. Not that you don’t have decent options - it’s just that everyone is trying to talk you out of using them.

NOTE: This section is still in progress. While waiting, I’d suggest that you have a look at this fantastic 11-post series below, which goes into detail on the well-known database access options in the Go world.
Golang Database Library and ORM Example - Introduction
Comparison between popular go libraries and ORM for database access layer.

When it comes to working with databases in Go, there are really only 3 viable options:

  • Talk to the DB directly, using the standard library’s db/sql package.
    • Can be very, very boilerplate-y.
  • Use a reflection-based ORM (ORM is a bit of a dirty word in the Go world, but yeah, a few of those exist).
  • Use code generation to ease your life and tuck the generated boilerplate away.

Helpful References #

Distributed SQLite with LiteFS
Using distributed SQLite and Go in the cloud with LiteFS.
sqlc.dev
Compile SQL to type-safe Go
ent: An entity framework for Go
Simple, yet powerful ORM for modeling and querying data.