package main import ( "fmt" "os" "github.com/joho/godotenv" "gorm.io/driver/postgres" "gorm.io/gorm" "log" "time" ) // User model type User struct { UserID uint `gorm:"primaryKey"` Username string `gorm:"size:50;not null"` Email string `gorm:"size:100;not null"` Password string `gorm:"size:100;not null"` CreatedAt time.Time `gorm:"autoCreateTime"` } func main() { // Database connection string err:= godotenv.Load(); if(err!=nil) { log.Fatal("Error loading .env file") } dsn := fmt.Sprint(os.Getenv("DATABASE_DSN")) log.Print(dsn) // Open the database connection db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { log.Fatal("failed to connect database:", err) } // Auto-migrate the schema db.AutoMigrate(&User{}) // Example of inserting a user user := User{Username: "john_doe", Email: "john@example.com", Password: "securepassword"} db.Create(&user) // At this point, user.UserID will be set to the ID generated by the database log.Println("New user ID:", user.UserID) }