Skip to content

Instantly share code, notes, and snippets.

@scastillo
Created October 17, 2019 02:23
Show Gist options
  • Save scastillo/06263259fba4d6f5df4b796567dbebdb to your computer and use it in GitHub Desktop.
Save scastillo/06263259fba4d6f5df4b796567dbebdb to your computer and use it in GitHub Desktop.
package models
import (
"db" //You have a storage layer wich init db and make is available on the namespace
)
type Model interface {
GetId() string
SetId(id string)
}
type modelImpl struct {
id string
}
func (m *modelImpl) SetId(id string) {
m.id = id
}
func (m *modelImpl) Save(debug bool) {
if debug {
db.Debug.Save(&m)
}else{
db.Save(&m)
}
}
package models
import ("db")
type User struct {
modelImpl
UserName string
FullName string
Email string
}
func NewUser(userName, fullName, email string) *User {
u := &User{
UserName: userName,
FullName: fullName,
Email: email,
}
u.SetId(userName) // lets say names are ids here so we can rewrite GetId
return u
}
func (u *User) GetId() string {
return u.UserName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment