Skip to content

Instantly share code, notes, and snippets.

@fervic
Created January 7, 2016 07:16
Show Gist options
  • Save fervic/ed4d74d79d62ff3dec55 to your computer and use it in GitHub Desktop.
Save fervic/ed4d74d79d62ff3dec55 to your computer and use it in GitHub Desktop.
Golang error checking closure
func httpRequestHandler(w http.ResponseWriter, req *http.Request) {
err := func () os.Error {
if req.Method != "GET" {
return os.NewError("expected GET")
}
if input := parseInput(req); input != "command" {
return os.NewError("malformed command")
}
} ()
if err != nil {
w.WriteHeader(400)
io.WriteString(w, err)
return
}
doSomething() ...
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment