Created
July 13, 2012 13:24
-
-
Save surma/3104871 to your computer and use it in GitHub Desktop.
Single-Line-Ifs
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 conditionalFailf(w http.ResponseWriter, fail bool, code int, format string, v ...interface{}) bool { | |
if fail { | |
log.Printf(format, v...) | |
http.Error(w, fmt.Sprintf(format, v...), code) | |
} | |
return fail | |
} | |
func createAccount(w http.ResponseWriter, r *http.Request) { | |
accountdata, account := Account{}, Account{} | |
dec := json.NewDecoder(r.Body) | |
e := dec.Decode(&accountdata) | |
if conditionalFailf(w, e != nil, http.StatusBadRequest, "Invalid request body: %s", e) { return } | |
//... | |
} | |
//... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment