Skip to content

Instantly share code, notes, and snippets.

@TrinhTrungDung
Last active March 20, 2020 12:04
Show Gist options
  • Save TrinhTrungDung/7b4ae164dc6a08394d2a7e3423dbe981 to your computer and use it in GitHub Desktop.
Save TrinhTrungDung/7b4ae164dc6a08394d2a7e3423dbe981 to your computer and use it in GitHub Desktop.
Add sample table and migrate to the database
package main
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
type User struct {
gorm.Model
Name string
Password string
}
func main() {
db, err := gorm.Open("postgres", "host=localhost port=5432 user=testuser dbname=testdb password=123456 sslmode=disable")
if err != nil {
fmt.Println(err)
}
defer db.Close()
fmt.Println("Successfully connect to new database")
db.AutoMigrate(&User{})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment