From 1476686cdb6acafdf62aad7672c3839bc4ca7033 Mon Sep 17 00:00:00 2001
From: Rob Pike
Library routines must often return some sort of error indication to
-the caller. As mentioned earlier, Go's multivalue return makes it
+the caller.
+As mentioned earlier, Go's multivalue return makes it
easy to return a detailed error description alongside the normal
-return value. By convention, errors have type
+By convention, errors have type error
,
+return value.
+It is good style to use this feature to provide detailed error information.
+For example, as we'll see, os.Open
doesn't
+just return a nil
pointer on failure, it also returns an
+error value that describes what went wrong.
+error
,
a simple built-in interface.
@@ -3301,7 +3310,12 @@ type error interface {
A library writer is free to implement this interface with a
richer model under the covers, making it possible not only
to see the error but also to provide some context.
-For example,
os.Open
returns an os.PathError
.
+As mentioned, alongside the usual *os.File
+return value, os.Open
also returns an
+error value.
+If the file is opened successfully, the error will be nil
,
+but when there is a problem, it will hold an
+os.PathError
:
// PathError records an error and the operation and