Skip to content

Instantly share code, notes, and snippets.

@surma
Created July 13, 2012 13:24
Show Gist options
  • Save surma/3104871 to your computer and use it in GitHub Desktop.
Save surma/3104871 to your computer and use it in GitHub Desktop.
Single-Line-Ifs
//...
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