Last active
March 26, 2021 11:36
-
-
Save yutakahashi114/555059f4f8b12de0bfacc6c13772c031 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"time" | |
_ "github.com/lib/pq" | |
"xorm.io/core" | |
"xorm.io/xorm" | |
) | |
func main() { | |
engine, err := xorm.NewEngine("postgres", "postgres://user:password@db/database?sslmode=disable") | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
defer engine.Close() | |
engine.ShowSQL(true) | |
engine.SetMapper(core.GonicMapper{}) | |
const count = 10 | |
us := make([]user, 0, count) | |
for i := 0; i < count; i++ { | |
us = append(us, user{ | |
FirstName: fmt.Sprintf("太郎%v", i), | |
LastName: fmt.Sprintf("テスト%v", i), | |
FirstNameKana: fmt.Sprintf("タロウ%v", i), | |
LastNameKana: fmt.Sprintf("テスト%v", i), | |
}) | |
} | |
err = insert(engine, us) | |
log.Println(err) | |
engine.Unscoped().Where("id > ?", 0).Delete(user{}) | |
} | |
type user struct { | |
ID uint64 `xorm:"autoincr"` | |
FirstName string | |
LastName string | |
FirstNameKana string | |
LastNameKana string | |
Version uint64 `xorm:"version"` | |
CreatedAt time.Time `xorm:"created"` | |
UpdatedAt time.Time `xorm:"updated"` | |
DeletedAt time.Time `xorm:"deleted"` | |
} | |
func (user) TableName() string { | |
return "users" | |
} | |
func insert(db xorm.Interface, us users) error { | |
_, err := db.Insert(&us) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment