Guide · Open source
How to use GORM: install, models and queries in Go
Updated · written with AI from the project's own documentation and checked against our catalogue data
GORM is an open-source ORM (object-relational mapper) for the Go programming language. It lets you describe your data as Go structs and then create, read, update, and delete records with Go code instead of hand-written SQL. This guide explains what GORM covers, how to get the source, and what to look at next so you can decide whether it fits your project.
Quick start
- 1Install Go. Install Go on your machine, since GORM is a Go library. Check the project documentation for the Go version it requires.
- 2Clone the repository. Run git clone https://github.com/go-gorm/gorm to get the source code.
- 3Read the official guides. Open https://gorm.io to learn model definitions, queries, and configuration.
- 4Define your model structs. Create Go structs that map to your database tables and fields.
- 5Run CRUD operations. Create, read, update, and delete records through GORM methods in your Go code.
- 6Add transactions and associations. Use transactions for grouped writes and associations to model relations between tables.
- 7Review migrations. Check what Auto Migrations will change before running them against a real database.
What GORM does and who it is for
GORM maps Go structs to database tables. You work with typed Go values, and GORM turns those operations into database queries. The README lists features including associations, hooks, transactions, eager loading, batch inserts, and auto migrations.
GORM is a good fit for Go developers building web services or APIs that store data in a relational database, and for teams that want one consistent, typed way to query tables from Go code. It is a Go-only library, so it is not useful if you are not writing Go.
- Associations: Has One, Has Many, Belongs To, Many To Many, Polymorphism, Single-table inheritance
- Transactions, nested transactions, save points, and RollbackTo a saved point
- Eager loading with Preload and Joins
- Batch Insert, FindInBatches, and Find To Map
- Auto Migrations and a Logger
- Plugin API, including Database Resolver for multiple databases and read/write splitting
Get the GORM source code
The README's install instruction is to clone the repository. Run the command below in a terminal. Go must be installed; the README excerpt does not state which Go version is required, so check the project's documentation for that.
The GORM guides at https://gorm.io are the README's main reference for usage.
git clone https://github.com/go-gorm/gormDefine a model as a Go struct
In GORM, a model is a Go struct that represents a table. Each field maps to a column. The README excerpt does not show model syntax, so refer to the official guides at https://gorm.io for field tags and conventions.
Keep models small and focused on one table. Associations between models, such as Has Many or Belongs To, are defined through the struct fields the guides describe.
Run create, read, update, and delete operations
The core workflow is CRUD: create records, find them, update them, and delete them, all through Go methods rather than raw SQL. The README highlights that GORM also supports Prepared Statement Mode, DryRun Mode, and Context, which help you inspect or control how statements run.
For complex queries, GORM offers a SQL Builder, Upsert, Locking, and NamedArg support. Use these when a plain find is not enough.
- Use Transactions when several writes must succeed or fail together.
- Use DryRun Mode to check generated statements without executing them.
- Use Preload or Joins to load related records efficiently.
Use auto migrations carefully
GORM includes Auto Migrations, which can create or update tables to match your structs. This speeds up early development. For production schemas, review what migrations will change before applying them and keep track of schema history yourself.
The README does not describe how migrations behave in detail, so read the official guides before relying on them.
Compare GORM with similar Go data tools
GORM is one of several ways to talk to a database from Go. Projects often compared with it include sqlc, ent, sqlx, and Bun. GORM emphasises a full-featured ORM with associations, hooks, and migrations. If you want full manual control over every SQL statement, a query builder or raw SQL may fit better.
What people use it for
- Building a REST or JSON API in Go that stores users, orders, or other records in a relational database.
- Writing a Go service that needs transactions across several related tables.
- Loading parent records with their children using Preload or Joins.
- Processing large tables in chunks with FindInBatches.
- Routing reads and writes to different databases with the Database Resolver plugin.
Frequently asked questions
Is GORM free to use?
Yes. GORM is released under the MIT License, as stated in the README. You can read the LICENSE file in the repository for the full terms.
What do I need to use GORM?
You need Go installed. The README excerpt does not specify a Go version, so check the official documentation. GORM is a Go-only library and is meant for relational databases.
How do I install GORM?
The README's install step is to clone the repository with git clone https://github.com/go-gorm/gorm. The install method is listed as package-manager, so check https://gorm.io for the recommended import path.
Is GORM a good alternative to writing raw SQL?
It can be for developers who want typed queries and fewer hand-written statements. If you need full manual control over every SQL statement, a query builder or raw SQL may fit better.
How does GORM compare to sqlx or ent?
GORM is a full-featured ORM with associations, hooks, transactions, and migrations. sqlx, ent, sqlc, and Bun are other Go options, and the right choice depends on how much abstraction you want. Compare them on the features you need.
Who should not use GORM?
People who do not write Go, complete beginners who should first learn Go and basic SQL, and projects that need complete control over every SQL statement should consider other tools.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
GORM suits Go developers building APIs or services on relational databases who want typed queries, associations, transactions, and migrations without writing SQL by hand. If you do not write Go, are new to programming, or need full control over raw SQL, pick a query builder such as sqlx or sqlc, or learn Go and SQL first.
Install options & downloads