Ukeje Goodness I am a data analyst who writes about cryptocurrencies and decentralized ledger technologies. Find me on Twitter @Goodylili.

Comparing the 7 best ORM packages in Go

5 min read 1453

Comparing The 7 Best ORM Packages In Go

Object-relational mappers (ORMs) are tools that provide functionalities for interacting with SQL databases using the data types and syntax of the preferred programming language. You can use an ORM to operate on databases without the hassle of writing pure SQL because a typical ORM provides database abstraction.

There are many benefits to using ORMs instead of databases using built-in data types. For example, you can use any supported database with an ORM without needing to make many changes to the existing code. Also, ORMs increase developer productivity and reduce development costs.

Go isn’t a purely object-oriented language, but the experience of using ORMs is similar to that of purely object-oriented languages, like Python and C++.

There are many ORM packages in the Go ecosystem; most use structs and interfaces for schemas depending on their approach.

This article overviews and compares seven ORM packages in the Go ecosystem to provide insights on the best ORM for your next project.

To jump ahead:

1. GORM

Gorm

The GORM (Go-ORM) package is the most popular ORM package in the Go ecosystem. GORM is a developer-friendly, feature-rich ORM built on the database/sql package, possessing many functionalities you’ll need in an ORM.

GORM provides functionalities for schema auto migration, logging, contexts, prepared statements, associations, constraints, advanced database operations like sharding, and much more.

The GORM package takes the code-first approach and uses structs as the model for interacting with databases. The package also provides functionalities for building raw SQL queries onto your database for unsupported operations.



To use the GORM package and interact with your database, you’ll need a database driver and the GORM package installed in your workspace. GORM provides a suite of database drivers for the popular SQL databases (MySQL, SQLite, MSSQL, PostgreSQL), and you can use custom database drivers to interact with your database.

Run this command in the terminal of your working directory to install the GORM package.

go get -u gorm.io/gorm

The GORM package is well-documented, with examples that overview all of its features, types, and methods. Check out the package documentation to learn more.

GORM is extremely developer-friendly and is popular because once a developer understands how to work with Go structs, the GORM package is intuitive to use.

Additionally, GORM doesn’t trade off most of the functionalities you’ll get from writing raw SQL queries.

2. SQLC: The SQL compiler

SQLC

SQLC is an SQL compiler ORM package that generates type-safe code from SQL to interact with SQL databases with Go data types. The SQLC package implements functionalities from the database/sql package data types, and you’ll find the package easy to use if you’ve worked with the sql package before.

SQLC takes the schema-first approach. After defining your schema and SQL queries, you’ll run the SQLC command to generate the type-safe code interfaces to your queries, and proceed to write code that calls the SQLC-generated code.

The SQLC package provides database queries, transactions, configurations, and functionalities.

Getting started with the SQLC package is a lengthy process. Depending on your operating system and the package, you’ll have to install the binaries on your machine.

Check out the official documentation page to learn more about the SQLC package, installation instructions, and how to use the package.

3. SQLX

SQLX

The SQLX library provides extensions to Go’s built-in database/sql package for more efficient, comprehensive interactions with databases in Go. The SQLX package is backward-compatible and extendable, so you can easily use it with the database/sql package and many other ORM packages.

The SQLX package takes the schema-first approach and provides functionalities for marshaling rows into structs with embedded struct support, and marshaling into maps and slices, prepared statements, and more.

You’ll find the SQLX package useful if you’re not a fan of full-featured ORMs and you prefer to use the database/sql package with extra support and functionalities.

Check out the SQLX official documentation for how to use the package and this guide for using the SQLX with the database/sql package.

4. Beego

Beego

The Beego ORM is a powerful ORM inspired by popular Python ORMs, like the Django ORM and SQLAlchemy, for easy database interactions in Go. The Beego ORM is part of the popular Beego web framework for building web apps in Go.

The Beego ORM takes the code-first approach and provides support for interacting with MySQL, PostgreSQL, and SQLite3 through the popular MySQL, PQ, and Go-Sqlite3 drivers.

The Beego ORM supports all Go data types, database queries, table joins, raw SQL queries, mapping, relationships, and more.

Interacting with databases using the Beego ORM is similar to using GORM, and the Beego ORM provides command line functionalities that are missing in GORM.


More great articles from LogRocket:


The Beego ORM is popular among developers that use the Beego framework for building web apps, one of the most popular frameworks in the Go ecosystem.

5. GORP

Gorp

GORP is a package for interacting with Go’s SQL databases using Go’s built-in data types. Though technically not an ORM, because Go isn’t objected-oriented, the Go Relational Persistence package has most of the functionalities provided by ORMs.

GORP creates mappings between Go structs and SQL tables, and you can use the package with multiple SQL dialects. The package supports embedded struct mapping, transactions, forward engineering database schemas from structs, hooks, database queries, slice binding, testing, logging, and much more.

The GORP package takes the code-first approach, and just like GORM, you can use tags for specifications. GORP uses reflections for constructing SQL queries and binding parameters without trading off much performance.

6. Go-firestorm

Go Firestorm

The Go-firestorm package is a fast, easy-to-use, non-intrusive, and non-exclusive ORM package in Go for the Google Cloud Firestore. The package supports the new Firestore Native mode and doesn’t provide support for the old Datastore mode.

The Go-firestorm package takes the code-first approach, supporting CRUD operations, searches, concurrent requests, nested transactions, cyclic references, sub-collections, embedded structs, custom mapping, caching, and operating on Google App Engine with Go versions greater than 1.11.

7. SQLBoiler

SQLBoiler

The SQLBoiler package is a schema-first ORM primed for type safety and performance while reducing the drawbacks of using ORMs. SQLboiler generates type-safe models from your defined database schema.

The SQLBoiler package provides functionalities for complete model generation, eager loading, raw SQL fallbacks, transactions, model hooks, multi-schema support, and handling complex table relationships.

You can check out this article to learn more about the SQLBoiler package and how you can use the ORM in your Go apps.

Comparing GO ORM packages

The ORMs in the Go ecosystem have varying features, functionalities, and areas of specialties.

Choosing an ORM will depend on the factors of the project you’re building, such as the project size, your preferred choice of database and ORM approach, and the popularity of the package.

If you will need help along the development process, you might prefer popular packages like GORM and Beego, which have many tutorials and more community support.

Here’s a table comparing the ORM packages discussed in this tutorial, their approaches, supported databases, developer experience, and popularity.

GORM SQLC SQLX Beego GORP Go-firestorm SQLBoiler
Approach Code-first Schema-first Schema-first Code-first Code-first Code-first Schema-first
Databases supported MySQL, MSSQL, PostgreSQL, SQLite, and many more database/sql supported databases database/sql supported databases MySQL, SQLite, PostgreSQL MySQL, SQLite, PostgreSQL, and more Google Cloud Firstore MySQL, MSSQL, PostgreSQL, SQLite, Cockroach DB
Developer experience Beginners to experts, very developer-friendly, and easy to use Primed for type safety It makes using the database/sql library worthwhile Popularly used with the Beego framework Easy to use code-first ORM Interacting with the Google Cloud Firestore from Go apps Performant, intuitive, and easy to use
Popularity (GitHub) 29.4k stars
497 watching
3.3k forks
6.2k stars
64 watching
427 forks
12.5k stars
196 watching
961 forks
28.8k stars
1.2k watching
5.5k forks
3.6k stars
111 watching
378 forks
35 stars
2 watching
7 forks
5.1k stars
77 watching
455 forks

Note: The Beego popularity data is from the web framework and not just the ORM.

GORM is the most popular ORM on the list, based on GitHub stars, and it is very developer-friendly while supporting many databases.

If you are unsure which ORM to choose, my advice is to go with GORM, Beego, or GORP if you prefer the code-first approach.

If you are comfortable with the database/sql package and you need extra functionalities, I would recommend SQLX or SQLC.

And if you prefer a schema-first approach and have type-safety and performance concerns, choose the SQLBoiler package.

Finally, I’d recommend the Go-firestorm package if you are working with the Google Cloud Firestore as your database.

Conclusion

This article has provided an overview of seven popular ORM packages in the Go ecosystem, their features, approaches, and more. Hopefully, with this information, you can choose the ORM that is best suited for your project.

Good luck!

proactively surfaces and diagnoses the most important issues in your apps and websites

Thousands of engineering and product teams use to reduce the time it takes to understand the root cause of technical and usability issues. With LogRocket, you will spend less time on back-and-forth conversations with customers and remove the endless troubleshooting process. LogRocket allows you to spend more time building new things and less time fixing bugs.

Be proactive - try today.

Ukeje Goodness I am a data analyst who writes about cryptocurrencies and decentralized ledger technologies. Find me on Twitter @Goodylili.

Leave a Reply