Last active
January 30, 2019 23:22
-
-
Save cn007b/ff10a781a30f6952be1406abdb67645d to your computer and use it in GitHub Desktop.
DRY in GO #1
This file contains 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
func GetEmployeeName(id string) (string, int) { | |
e, err := db.GetEmployeeByID(id) | |
if err != nil { | |
httpErrorCode := 500 | |
switch err { | |
case ErrorInvalidID: | |
httpErrorCode = 400 | |
case ErrorUnauthorized: | |
httpErrorCode = 401 | |
case ErrorEmployeeNotFound: | |
httpErrorCode = 404 | |
// other errors | |
// ... | |
} | |
return "", httpErrorCode | |
} | |
return e.Name, 200 | |
} | |
func GetEmployeeEmail(id string) (string, int) { | |
e, err := db.GetEmployeeByID(id) | |
if err != nil { | |
httpErrorCode := 500 | |
switch err { | |
case ErrorInvalidID: | |
httpErrorCode = 400 | |
case ErrorUnauthorized: | |
httpErrorCode = 401 | |
case ErrorEmployeeNotFound: | |
httpErrorCode = 404 | |
// other errors | |
// ... | |
} | |
return "", httpErrorCode | |
} | |
return e.Email, 200 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment