Skip to content

Commit 027dc7f

Browse files
authored
Merge pull request #58 from tsingbx/error
Add doc for error interface
2 parents 14635fe + b8b723b commit 027dc7f

File tree

6 files changed

+50
-4
lines changed

6 files changed

+50
-4
lines changed

217-Interface-Values/interface-values-3.gop

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010

1111
var w io.Writer
1212

13-
fmt.printf "%T\n", w // "<nil>"
13+
fmt.printf("%T\n", w) // "<nil>"
1414

1515
w = os.Stdout
16-
fmt.printf "%T\n", w // "*os.File"
16+
fmt.printf("%T\n", w) // "*os.File"
1717

1818
w = new(bytes.Buffer)
19-
fmt.printf "%T\n", w // "*bytes.Buffer"
19+
fmt.printf("%T\n", w) // "*bytes.Buffer"
2020

2121
// Internally, fmt uses reflection to obtain the name of the int erface’s dynamic type.

218-The-error-Interface/error-1.gop

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// error interface has a single method that returns an error message:
2+
3+
type error interface {
4+
Error() string
5+
}

218-The-error-Interface/error-2.gop

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// The simplest way to create an error is by calling errors.New, which returns a new error for
2+
// a given error message. The entire errors package is only four lines long:
3+
4+
package errors
5+
6+
func New(text string) error {
7+
return &errorString{text}
8+
}
9+
10+
type errorString struct {
11+
text string
12+
}
13+
14+
func (e *errorString) Error() string {
15+
return e.text
16+
}
17+
18+
// The underlying type of errorString is a struct, not a string , to protect its representation from
19+
// inadvertent (or premeditated) updates. And the reason that the pointer type *errorString,
20+
// not errorString alone, satisfies the error interface is so that every call to New allocates a distinct error instance that is equal to no other.

218-The-error-Interface/error-3.gop

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// We would not want a distinguished error such as io.EOF to compare equal to one that merely happened to have the same message.
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
fmt.Println(errors.New("EOF") == errors.New("EOF")) // "false"

218-The-error-Interface/error-4.gop

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Calls to errors.New are relatively infrequent because there’s a convenient wrapper function,
2+
// fmt.Errorf, that does string formatting too.
3+
4+
package fmt
5+
6+
import "errors"
7+
8+
func Sprintf(format string, args ...interface{}) (s string) {
9+
//...
10+
}
11+
12+
func Errorf(format string, args ...interface{}) error {
13+
return errors.New(Sprintf(format, args...))
14+
}

218-The-error-Interface/error.gop

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)