- Install Glide.
- Clone this gist to a local directory.
- Run
glide installto resolve all the dependencies. - Run
go run mysql_example.go
-
-
Save nasutionam/b926c6f6da1f191be0815b6a8b308a6c to your computer and use it in GitHub Desktop.
Golang MySQL ORM Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MYSQL_USER="root" | |
| MYSQL_PASSWORD="" | |
| MYSQL_DBNAME="gorm_test" | |
| MYSQL_PROTOCOL="tcp" | |
| MYSQL_HOST="127.0.0.1" | |
| MYSQL_PORT="3306" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package: github.com/itsprdp/goorm-example | |
| import: | |
| - package: github.com/go-sql-driver/mysql | |
| version: ~1.3.0 | |
| - package: github.com/jinzhu/gorm | |
| version: ~1.0.0 | |
| - package: github.com/joho/godotenv | |
| version: ~1.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| _ "github.com/go-sql-driver/mysql" | |
| "github.com/jinzhu/gorm" | |
| "github.com/joho/godotenv" | |
| ) | |
| type User struct { | |
| gorm.Model | |
| FirstName string | |
| LastName string | |
| Email string `gorm:"unique_index:user_email_index"` | |
| Password string | |
| Token string | |
| TokenExpiresAt uint | |
| } | |
| func main() { | |
| var appConfig map[string]string | |
| appConfig, err := godotenv.Read() | |
| if err != nil { | |
| log.Fatal("Error reading .env file") | |
| } | |
| // Ex: user:password@tcp(host:port)/dbname | |
| mysqlCredentials := fmt.Sprintf( | |
| "%s:%s@%s(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", | |
| appConfig["MYSQL_USER"], | |
| appConfig["MYSQL_PASSWORD"], | |
| appConfig["MYSQL_PROTOCOL"], | |
| appConfig["MYSQL_HOST"], | |
| appConfig["MYSQL_PORT"], | |
| appConfig["MYSQL_DBNAME"], | |
| ) | |
| db, err := gorm.Open("mysql", mysqlCredentials) | |
| defer db.Close() | |
| if err != nil { | |
| fmt.Println(err) | |
| panic("Failed to connect to the database!") | |
| } | |
| if db.HasTable(&User{}) == false { | |
| db.CreateTable(&User{}) | |
| } | |
| user := User{ | |
| FirstName: "John", | |
| LastName: "Doe", | |
| Email: "[email protected]", | |
| Password: "insecurepassword", | |
| } | |
| db.Create(&user) | |
| var users []User | |
| db.Find(&users) | |
| fmt.Println("There are", len(users), "user records in the table.") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment