4 ms·
I just picked a random-ish file from the standard library to see what the error handling really looks like in practice (I went for "io" because I figured that's
by iainmerrick 5y ago
I just picked a random-ish file from the standard library to see what the error handling really looks like in practice (I went for "io" because I figured that's where errors will always crop up): https://cs.opensource.google/go/go/+/master:src/io/fs/glob.go https://cs.opensource.google/go/go/+/master:src/io/fs/glob.g...
Does that seem reasonably representative? To a non-Go programmer, it doesn't seem that bad, but it sure does have a lot of error-handling boilerplate -- squinting at it, something around 50% of the non-comment lines? It just seems kind of weird, if you have to write "err != nil" and "return nil, err" over and over, not to have any syntactic sugar for it!
There's what looks like missing return values on lines 64 and 69, then down on line 93 there's an explicit "return // ignore I/O error".
Is this file outdated, or otherwise not a good example? I used Google to find it, and it says "Copyright 2020" in the header.
- llimllib 5y agoit's not outdated, and it's a fine example - reading the go stdlib is the best way to learn the language well, honestly. The missing return values on 64 and 69 are the "Named Return Values" thing that OP mentions - the returns are the variables named in the function declaration.
- iainmerrick 5y agoAha, that explains it, thanks!