Created
October 17, 2019 02:23
-
-
Save scastillo/06263259fba4d6f5df4b796567dbebdb to your computer and use it in GitHub Desktop.
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 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) | |
} | |
} |
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 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