Skip to content

Instantly share code, notes, and snippets.

@xealgo
Created January 19, 2018 17:21
Show Gist options
  • Save xealgo/ebb7e8678358fb6977400653d9f4c037 to your computer and use it in GitHub Desktop.
Save xealgo/ebb7e8678358fb6977400653d9f4c037 to your computer and use it in GitHub Desktop.
// store/rdb/user.go
// User satisfies user store interface
type User struct {
db *rdb.DB
}
func New(db *rdb.DB) User {
return &User{db: db}
}
func (u *User) Get(..) {
...
}
func (u *User) Save(..) {
...
}
func (u *User) Remove(..) {
...
}
// api/users.go
type Users struct {
userStore users.Store
...
}
func New(store users.Store, ...) Users {
return &Users{userStore: store}
}
func (u *Users) SomeHandler(ctx context.Context) {
user, err := u.userStore.Get(1)
...
}
// maybe go further also...(I haven't done this yet)
// users/users.go
type User struct {
store Store
Name string
...
}
func (u *User) Get(id uint64) {
store.Get(id, &u)
...
}
// main.go
func main() {
db := rdb.New(...)
us := rdb.New(db)
uc := api.New(us)
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment