2026-01-11 10:11:09 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type User struct {
|
2026-01-13 00:08:49 +08:00
|
|
|
Id int8
|
|
|
|
|
CreateTime time.Time
|
|
|
|
|
LastUpdateTime time.Time
|
|
|
|
|
Name string
|
|
|
|
|
Account string
|
|
|
|
|
Passwd string
|
2026-01-11 10:11:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
2026-01-13 00:08:49 +08:00
|
|
|
fmt.Println("程序启动!")
|
2026-01-11 10:11:09 +08:00
|
|
|
dsn := "host=localhost " +
|
|
|
|
|
"user=gatekeeper " +
|
|
|
|
|
"dbname=crimson " +
|
|
|
|
|
"sslmode=disable " +
|
|
|
|
|
"port=5432 " +
|
2026-01-13 00:08:49 +08:00
|
|
|
"password=crimson " +
|
|
|
|
|
"connect_timeout=20 "
|
2026-01-11 10:11:09 +08:00
|
|
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic("无法连接数据库")
|
|
|
|
|
}
|
2026-01-13 00:08:49 +08:00
|
|
|
fmt.Println("数据库连线完成")
|
2026-01-11 10:11:09 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
2026-01-12 18:38:40 +08:00
|
|
|
usr, err := gorm.G[User](db).First(ctx)
|
2026-01-13 00:08:49 +08:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2026-01-12 18:38:40 +08:00
|
|
|
fmt.Println(usr)
|
2026-01-11 10:11:09 +08:00
|
|
|
fmt.Println("程序结束")
|
|
|
|
|
}
|