GORM is an object-relational mapping (ORM) library for Go that lets developers work with databases through Go models and operations. The source lesson introduces GORM from initial installation through associations and preloading. The steps below summarize the lesson’s topics; check current GORM documentation before applying version-specific APIs in a production project.
Install GORM and a database driver
The lesson begins by adding GORM and a driver with go get. Its example uses MySQL, with gorm.io/gorm and gorm.io/driver/mysql. Other drivers, such as PostgreSQL or SQLite, may be appropriate for different projects.
Connect to a database
GORM’s Open function takes a driver and connection details such as username, password, host, and database name. Create the database before connecting, using a database tool or SQL. The sample connection string names a local MySQL host, port 3306, database, UTF-8 character set, and time settings. Keep real credentials out of source code.
Models and migrations
A GORM model is a Go struct that represents data stored in a table. The lesson covers creating tables from models with migration functions, including AutoMigrate. It demonstrates a Gender model and migration, then introduces GORM conventions such as pluralized snake_case table names and snake_case columns.
The lesson also names logger configuration, interfaces, custom configuration, and struct tags for setting a column name, primary key, index, comment, and table name. Verify the exact tag and migration behavior against the GORM version used by your project.
CRUD operations
The lesson introduces create, read, update, and delete operations:
Createinserts a new record, with aGenderexample.Firstretrieves the first record matching a condition, such as an integer primary key.Takeretrieves one record without the ordering behavior ofFirst.Lastretrieves the last matching record.- The source notes that GORM has no
Filefunction. Wherefilters records; the lesson uses a parameterized name condition.Ordersorts results, andLimitcaps the number returned.Updatechanges one field;Updateschanges multiple fields.
The examples show selecting a gender by ID or name and changing a name from “Male” to “Female.” Confirm the behavior of each operation and handle returned errors in real code.
Delete records
The lesson covers Delete, GORM soft deletes through a deleted_at field such as gorm.DeletedAt, and permanent deletion with Unscoped().Delete(). A permanent delete is destructive, so make sure the scope and data-retention requirements are understood before using it.
Associations and preloading
GORM associations connect related models. The lesson uses a Customer model with a GenderID field and a related Gender, then demonstrates Preload("Gender") when retrieving a customer. Preloading fetches related data alongside the main record and can reduce separate lookup calls, depending on the query and association.
The source lesson’s code examples are flattened in the saved article text. Treat this page as a topic outline and verify syntax against the current GORM documentation before copying code.