Initialize GORM by opening a connection and calling AutoMigrate on your struct to create the database table.
Use GORM by importing the package, opening a database connection, defining a struct model, and calling AutoMigrate to create the table.
package main
import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type User struct {
ID uint
Name string
}
func main() {
db, _ := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
db.AutoMigrate(&User{})
}
GORM is a tool that lets you talk to a database using Go code instead of writing raw SQL commands. It automatically creates your database tables based on your Go data structures. Think of it as a translator that turns your Go variables into database rows so you can focus on building your app.