Errors

An error is a value

Swift throws. Go does not. In Go, an error is an ordinary value you return alongside your result β€” there is no try/catch, no stack unwinding for the everyday case. A function that might fail returns two things: the result and an error.

The convention is ironclad: the error is the last return value, and its type is the built-in interface error. When everything worked, the error is nil. When something went wrong, it's a non-nil error describing what.

error is itself just an interface β€” one method, Error() string. Anything that can describe itself as a string is an error. (Yes, this is exactly the interface idea from Lesson 1.)