sgo provides easy and fun way to interact with sql database
go get github.com/srajelli/sgo
Open a DB connection
db, err := sgo.Open("mysql", "username:pass@/dbname")
Just define tag names to your fields. Tag names is equal to column names of the table
type User struct {
Name string `sql:"name"`
Email string `sql:"email"`
AccessToken string `sql:"accesstoken"`
}
user := User{}
db.Table("user").
Where("plan = 'basic'").
And("name = 'John'").
Or("email = 'john.due@gmail.com'").
Get(&user)
fmt.Println(user)
user := User{}
user.Name = "John Due"
user.Email = "john.due@gmail.com"
user.Plan = "basic"
db.Table("users").
Insert(&user)
user := User{}
user.Plan = "pro"
db.Table("users").Table("users").
Where("email = 'john.due@gmail.com'").
Update(&user)
db.Table("users").Table("users").
Where("email = 'john.due@gmail.com'").
Delete()