3 ms·
The link in the "Error variable naming" section seems to directly contradict what the section says. The link (https://go.dev/talks/2014/names.slide#14 https://
by Mogzol 2y ago
The link in the "Error variable naming" section seems to directly contradict what the section says.
The link (https://go.dev/talks/2014/names.slide#14 https://go.dev/talks/2014/names.slide#14) says:
Error values should be of the form ErrFoo:
var ErrFormat = errors.New("image: unknown format")
But the page says:
// But if you want to give it a longer name, use "somethingError".
var specificError error
result, specificError = doSpecificThing()
And also says:
Don't do this:
[...]
var errSpecific error
result, errSpecific = doSpecificThing()
So should error variables written like `errSpecific` or `specificError`? The go wiki says they should be written starting with `err`: https://go.dev/wiki/Errors#naming https://go.dev/wiki/Errors#naming
- DHowett 2y agoThe advice in the 2014 talk applies to exporting specific types of errors as part of a package’s public API surface. The article, on the other hand, advocates for local variables storing errors not to have distinct names.
- Mogzol 2y agoSo a public variable error should follow different naming conventions then a local variable? That doesn't seem right, the go wiki says you should use the 'err' prefix for both (capitalized for public variables though, obviously) And I'm only asking about when you are giving an error a distinct name, not just naming it 'err'.