ORM
Object-Relational Mapping (ORM) tools and query builders for Go.
Overview
The orm package provides working examples and comprehensive documentation for five popular Go database abstraction libraries. Each tool represents a different philosophy for database interaction, from full-featured ORMs to lightweight query builders.
Quick Comparison
Philosophy
| Tool |
Approach |
Schema Definition |
Query Style |
| Ent |
Code-first |
Go structs with field builders |
Graph-based, type-safe |
| GORM |
Convention over config |
Go structs with tags |
ActiveRecord-style |
| SQLC |
SQL-first |
SQL schema files |
Raw SQL with codegen |
| SQLx |
Database/sql wrapper |
Manual SQL |
Raw SQL with scanning |
| Beego |
Framework-integrated |
Go structs with tags |
QueryBuilder + ORM |
Feature Matrix
| Feature |
Ent |
GORM |
SQLC |
SQLx |
Beego |
| Type Safety |
β
β
β
|
β
β
|
β
β
β
|
β
|
β
β
|
| Learning Curve |
Steep |
Easy |
Medium |
Easy |
Medium |
| Code Generation |
β
|
β |
β
|
β |
β |
| Auto Migration |
β
|
β
|
β |
β |
β
|
| Relationships |
β
β
β
|
β
β
β
|
Manual |
Manual |
β
β
|
| Performance |
β
β
|
β
|
β
β
β
|
β
β
β
|
β
β
|
| SQL Control |
β
|
β
|
β
β
β
|
β
β
β
|
β
β
|
| Community Size |
Medium |
Large |
Medium |
Large |
Small |
Decision Guide
Choose Ent When
- Complex relationships: Your domain has many entity relationships
- Type safety critical: Compile-time guarantees are essential
- Graph queries: You need to traverse relationships efficiently
- Schema evolution: Automatic migration generation is valuable
- Greenfield projects: Starting fresh with code-first approach
π Read the Ent Guide
Choose GORM When
- Rapid development: Need to build quickly with minimal boilerplate
- Familiar API: Coming from Rails/ActiveRecord background
- Rich features: Need hooks, associations, auto-migration out of the box
- Large community: Want extensive documentation and plugins
- Learning curve: Need something easy to pick up
π Read the GORM Guide
Choose SQLC When
- SQL expertise: You know SQL and want to write it directly
- Performance critical: Need zero runtime overhead
- Type safety: Want compile-time query verification
- Database-specific: Need to use database-specific features
- Simple queries: Mostly straightforward CRUD operations
π Read the SQLC Guide
Choose SQLx When
- Minimal abstraction: Want thin layer over database/sql
- Full control: Need complete control over SQL
- Small projects: Don't need heavy ORM machinery
- Learning SQL: Want to improve SQL skills
- Existing code: Migrating from database/sql
π Read the SQLx Guide
Choose Beego ORM When
- Beego framework: Already using Beego web framework
- Framework integration: Want tight web framework coupling
- QueryBuilder needed: Mix of ORM and query builder
- Multi-database: Need to support multiple databases easily
- China-focused: Development team is China-based
π Read the Beego Guide
Getting Started
See individual guides for detailed installation, configuration, and usage:
- Ent Guide - Schema definition, code generation, graph queries
- GORM Guide - Models, associations, migrations, hooks
- SQLC Guide - SQL queries, code generation, configuration
- SQLx Guide - Struct scanning, named queries, transactions
- Beego Guide - ORM features, QueryBuilder, framework integration
| Tool |
Query Performance |
Memory Usage |
Startup Time |
| Ent |
Good |
Medium |
Medium (codegen) |
| GORM |
Fair |
High |
Fast |
| SQLC |
Excellent |
Low |
Medium (codegen) |
| SQLx |
Excellent |
Low |
Fast |
| Beego |
Good |
Medium |
Fast |
Recommendations:
- High performance: SQLC or SQLx for minimal overhead
- Balanced: Ent for type safety with good performance
- Rapid development: GORM for quick iteration
- Control: SQLx for manual optimization
Common Patterns
Connection Management
// Ent
client, err := ent.Open("mysql", dsn)
defer client.Close()
// GORM
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
sqlDB, _ := db.DB()
defer sqlDB.Close()
// SQLC/SQLx
db, err := sqlx.Connect("mysql", dsn)
defer db.Close()
// Beego
orm.RegisterDataBase("default", "mysql", dsn)
Transaction Handling
// Ent
tx, err := client.Tx(ctx)
if err := txFunc(ctx, tx); err != nil {
tx.Rollback()
} else {
tx.Commit()
}
// GORM
db.Transaction(func(tx *gorm.DB) error {
// operations
return nil
})
// SQLx
tx, _ := db.Beginx()
defer tx.Rollback()
// operations
tx.Commit()
From database/sql to SQLx
Easiest migration - SQLx extends database/sql interface.
From GORM to Ent
Requires schema redesign but improves type safety.
From Raw SQL to SQLC
Keep your SQL, gain type safety through codegen.
Always possible - SQLx works with any database/sql driver.
Next Steps
Choose a tool from the decision guide above and read its detailed documentation. Each guide includes installation, complete API reference, best practices, testing examples, and troubleshooting.
Resources