In the GO language specification its said things which needs to be exported outside package must starts with capital letter. I am wondering how error
interface is exposed outside and can be accessible anywhere even though it starts with small letter unlike other interfaces starts with capital letter like Stringer
.
error
is a builtin type just like int
, bool
, string
etc. I guess you've never wondered why int
is available despite starting with a lowecased letter.
Builtin types are predeclared identifiers, they are implicitly declared in the universe block and so available everywhere without any imports or qualifiers.
error
is a special case, defined in the language spec:
The predeclared type error is defined as
type 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)
As historical trivia, in a pre-release version of Go, it was part of a standard library package, but this lead to a dependency nightmare, so they made it a special case.