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