How to Use GORM

A Complete Guide for Go

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{})
}