Skip to content

Commit

Permalink
增加GORM的demo
Browse files Browse the repository at this point in the history
  • Loading branch information
zq2599 committed Feb 13, 2023
1 parent 205a5f4 commit 7797dde
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ jetcd-tutorials/.gradle/*
.DS_Store
grpc-tutorials/.gradle/*
leetcode/out
leetcode/.vscode/*
leetcode/.vscode/*
blog_demos/tutorials/gorm-tutorials/test.db
11 changes: 11 additions & 0 deletions tutorials/gorm-tutorials/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module gorm-tutorials

go 1.19

require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
gorm.io/driver/sqlite v1.4.4 // indirect
gorm.io/gorm v1.24.5 // indirect
)
13 changes: 13 additions & 0 deletions tutorials/gorm-tutorials/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
gorm.io/driver/sqlite v1.4.4 h1:gIufGoR0dQzjkyqDyYSCvsYR6fba1Gw5YKDqKeChxFc=
gorm.io/driver/sqlite v1.4.4/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI=
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.24.5 h1:g6OPREKqqlWq4kh/3MCQbZKImeB9e6Xgc4zD+JgNZGE=
gorm.io/gorm v1.24.5/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
40 changes: 40 additions & 0 deletions tutorials/gorm-tutorials/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"fmt"

"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

type Product struct {
gorm.Model
Code string
Price uint
}

const (
FIRST_PRODUCT_CODE = "D101"
)

func main() {
fmt.Println("Hello world")

db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})

if err != nil {
panic("failed to connect database")
}

db.AutoMigrate(&Product{})

db.Create(&Product{Code: FIRST_PRODUCT_CODE, Price: 1000})

var product Product

db.First(&product, "code = ?", FIRST_PRODUCT_CODE)

fmt.Printf("product: %+v\n", product)

db.Delete(&product, product.ID)
}
Binary file added tutorials/gorm-tutorials/test.db
Binary file not shown.

0 comments on commit 7797dde

Please sign in to comment.