Back to list

GORM Database Operations

Lv.5552@mukitaro14 playsDec 28, 2025

GORM ORM for Go. Most popular Go ORM library.

preview.go
Go
1package main
2
3import (
4 "gorm.io/driver/postgres"
5 "gorm.io/gorm"
6)
7
8type Product struct {
9 gorm.Model
10 Code string `gorm:"uniqueIndex"`
11 Price uint
12}
13
14func main() {
15 dsn := "host=localhost user=postgres password=secret dbname=mydb"
16 db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
17 if err != nil {
18 panic("failed to connect database")
19 }
20
21 db.AutoMigrate(&Product{})
22
23 db.Create(&Product{Code: "D42", Price: 100})
24
25 var product Product
26 db.First(&product, "code = ?", "D42")
27
28 db.Model(&product).Update("Price", 200)
29
30 db.Delete(&product)
31}

Custom problems are not included in rankings