Created
January 20, 2015 00:12
-
-
Save danielstjules/2c93a39b2748059394ac to your computer and use it in GitHub Desktop.
Error handling
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
type Foo struct { | |
Bar int | |
Baz string | |
Qux *exec.Cmd | |
} | |
func NewFoo() (*Foo, error) { | |
bar, err := getBar() | |
if err != nill { | |
return nil, err | |
} | |
baz, err := getBaz() | |
if err != nill { | |
return nil, err | |
} | |
qux, err := getQux() | |
if err != nill { | |
return nil, err | |
} | |
return &Foo{bar, baz, qux}, nil | |
} | |
// Imagine that the functions are defined below... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I guess a similar example in JS using promises isn't really shorter:
But if sync, standard try/catch is a bit more succinct: