Skip to content

Instantly share code, notes, and snippets.

@cn007b
Last active January 30, 2019 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cn007b/06f2c1bdb28b6300b68238d791fad289 to your computer and use it in GitHub Desktop.
Save cn007b/06f2c1bdb28b6300b68238d791fad289 to your computer and use it in GitHub Desktop.
DRY in GO #3
func withEmployee(id string, cb func(Employee) (string, int)) (string, int) {
e, err := db.GetEmployeeByID(id)
if err != nil {
// error handling provided earlier
}
return cb(e)
}
func GetEmployeeName(id string) (string, int) {
return withEmployee(id, func(e Employee) (s string, i int) {
return e.Name, 200
})
}
func GetEmployeeEmail(id string) (string, int) {
return withEmployee(id, func(e Employee) (s string, i int) {
return e.Email, 200
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment