Today we’re looking at errors, and the built-in error
interface. For a concept that’s so central to the Go ethos, the spec has surprisingly little to say about it:
Errors
The predeclared type
error
is defined astype error interface { Error() string }
It is the conventional interface for representing an error condition, with the nil value representing no error. For instance, a function to read data from a file might be defined:
func Read(f *File, b []byte) (n int, err error)
Why is there so little to say about errors in the spec?
Most of what we do with errors, isn’t “part of the language”. The only thing that really is baked into the language is the pre-declared type error
. So that’s all we get here. 😊
Of course there’s a ton more that could be said about errors. And once we finish the spec (which is imminent now!), I’ll be happy to dive into such topics.
If you have any questions about errors, this is a great time to hit « REPLY » and let me know. I can start a backlog of interesting topics to discuss in the next week or two, when this spec series finishes up.
Quotes from The Go Programming Language Specification Language version go1.23 (June 13, 2024)