package main import ( "fmt" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" "time" ) // 定义 User 模型 type User struct { gorm.Model Name string Email string Test Test } type Model struct { ID uint `gorm:"primarykey"` CreatedAt *time.Time UpdatedAt *time.Time DeletedAt gorm.DeletedAt `gorm:"index"` } type Test struct { Model Name string } func main() { t := Test{ Name: "test", } Created(&t) // 打印成功创建的消息 fmt.Printf("User created with ID: %d\n", t.ID) } func Created(u *Test) { dsn := "root:lxz664278@tcp(106.54.33.152:33369)/gorm?charset=utf8mb4" db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ Logger: logger.Default.LogMode(logger.Info), NowFunc: func() time.Time { location, _ := time.LoadLocation("America/New_York") return time.Now().In(location) }, }) if err != nil { panic("failed to connect database") } db.Model(Test{}).Create(u) }