GORM là ORM phổ biến nhất trong Go, cung cấp API trực quan để thao tác database: db.Create(&user) để insert, db.First(&user, 1) để query theo primary key, và db.Where("age > ?", 18).Find(&users) cho query có điều kiện.
GORM hỗ trợ auto migration (db.AutoMigrate(&User{})) để tự tạo/cập nhật bảng theo struct definition, cùng với các associations như BelongsTo, HasMany, ManyToMany và hooks như BeforeCreate, AfterUpdate để chạy logic trước/sau thao tác.
Với các query phức tạp hoặc cần kiểm soát performance chặt, nhiều team chọn dùng sqlx (raw SQL kết hợp auto scanning) hoặc ent (Facebook, code generation) thay thế.
GORM is the most popular ORM in Go, offering an intuitive API: db.Create(&user) to insert, db.First(&user, 1) to query by primary key, and db.Where("age > ?", 18).Find(&users) for conditional queries.
GORM supports auto-migration (db.AutoMigrate(&User{})) to create/update tables from struct definitions, along with associations (BelongsTo, HasMany, ManyToMany) and lifecycle hooks (BeforeCreate, AfterUpdate).
For complex queries or strict performance requirements, many teams prefer sqlx (raw SQL with auto-scanning) or ent (Facebook's code-generation ORM).