GORM in Golang: Structs to Database Tables Made Easy | Go Tutorial #34

Опубликовано: 27 Март 2026
на канале: Taught by Celeste AI - AI Coding Coach
45
2

Learn GORM, the most popular ORM for Go! Map Go structs to database tables automatically using gorm.io/gorm with a pure Go SQLite driver —
no CGO required.

📚 What you'll learn:
`gorm.Open(sqlite.Open("file.db"))` to connect
`gorm.Model` for auto ID, CreatedAt, UpdatedAt, DeletedAt
`db.AutoMigrate(&User{})` to create tables from structs
`db.Create(&user)` with auto-populated ID
`db.First(&user, 1)` and `db.Find(&users)` for queries
`db.Model().Update()` and `db.Model().Updates()` for modifications
Soft delete with `db.Delete()` and `db.Unscoped()` for recovery
`db.Where("done = ?", true).Find()` for filtered queries
Has Many / Belongs To associations with `UserID` convention
`db.Preload("Posts").First()` for eager loading
Nested create: user with posts in one call
`BeforeCreate` / `AfterCreate` lifecycle hooks
Scopes: reusable query filters like `Expensive` and `NotDiscounted`
`db.Raw()` and `db.Exec()` for raw SQL when needed
`db.Transaction()` with automatic rollback on error

⏱️ Timestamps:
0:00 - Introduction
0:30 - GORM Basics (Connect, AutoMigrate, Create, Find)
3:44 - CRUD Operations (Update, Delete, Soft Delete, Unscoped)
8:19 - Associations (Has Many, Belongs To, Preload)
12:32 - Advanced Features (Hooks, Scopes, Raw SQL, Transactions)
18:12 - Recap
18:42 - Up Next: REST API

💻 Code: https://github.com/GoCelesteAI/go_gor...