0PricingLogin
Go Academy · Lesson

CRUD Operations

Create, read, update, delete.

CRUD with GORM

GORM turns the four basic operations, Create, Read, Update, Delete, into method calls on *gorm.DB. You pass pointers to structs and GORM builds the SQL.

Create

Create inserts a record. After it runs, GORM fills the structs primary key and timestamps from the database.

user := User{Name: "Ada", Email: "ada@example.com"}
result := db.Create(&user)
fmt.Println(user.ID, result.RowsAffected)

All lessons in this course

  1. Models and AutoMigrate
  2. CRUD Operations
  3. Associations
  4. Hooks and Scopes
← Back to Go Academy