How to use GORM

Initialize GORM by opening a connection, defining your data models as structs, and using built-in methods to create, read, update, and delete records.

Use GORM by importing the gorm.io/gorm package, opening a database connection, and defining structs that map to your database tables.

import "gorm.io/gorm"

db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
    panic("failed to connect database")
}

type User struct {
    ID   uint
    Name string
}

// Auto-migrate the schema
db.AutoMigrate(&User{})

// Create a record
db.Create(&User{Name: "Jinzhu"})

// Query a record
var user User
db.First(&user, 1)