Fix grammar for Error Type section in 11.1
This commit is contained in:
@@ -15,7 +15,7 @@ Similar to the `os.Open` function, the functions in Go's standard packages all r
|
|||||||
|
|
||||||
## Error type
|
## Error type
|
||||||
|
|
||||||
`error` is an interface type, with this definition:
|
`error` is an interface type with the following definition:
|
||||||
|
|
||||||
type error interface {
|
type error interface {
|
||||||
Error() string
|
Error() string
|
||||||
@@ -23,6 +23,10 @@ Similar to the `os.Open` function, the functions in Go's standard packages all r
|
|||||||
|
|
||||||
`error` is a built-in interface type, we can / builtin / pack below to find the appropriate definition. And we have a lot of internal error is used inside the package packet errors following implementation structure errorString private
|
`error` is a built-in interface type, we can / builtin / pack below to find the appropriate definition. And we have a lot of internal error is used inside the package packet errors following implementation structure errorString private
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
`error` is a built-in interface type. We can find the corresponding definition in the builtin package below. We also have a lot of internal packages using the error in a private structure called `errorString`, which implements the error interface:
|
||||||
|
|
||||||
// errorString is a trivial implementation of error.
|
// errorString is a trivial implementation of error.
|
||||||
type errorString struct {
|
type errorString struct {
|
||||||
s string
|
s string
|
||||||
@@ -32,7 +36,7 @@ Similar to the `os.Open` function, the functions in Go's standard packages all r
|
|||||||
return e.s
|
return e.s
|
||||||
}
|
}
|
||||||
|
|
||||||
You can `errors.New` put a string into errorString, in order to get a meet the interface error object whose internal implementation is as follows:
|
You can convert a regular string to an `errorString` through `errors.New` in order to get an object that satisfies the error interface. Its internal implementation is as follows:
|
||||||
|
|
||||||
// New returns an error that formats as the given text.
|
// New returns an error that formats as the given text.
|
||||||
func New(text string) error {
|
func New(text string) error {
|
||||||
@@ -48,7 +52,7 @@ The following example demonstrates how to use `errors.New`:
|
|||||||
// implementation
|
// implementation
|
||||||
}
|
}
|
||||||
|
|
||||||
In the following example, we call the Sqrt when passing a negative number , and then you get the error object is non-nil , nil compared with this object , the result is true, so `fmt.Println` (fmt package when dealing with error calls the error method ) is called to output error , look at the following sample code to call :
|
In the following example, we pass a negative number to our `Sqrt` function. Checking the `err` variable, we check whether the error object is non-nil using a simple nil comparison. The result of the comparison is true, so `fmt.Println` (the `fmt` package calls the error method when dealing with error calls) is called to output an error.
|
||||||
|
|
||||||
f, err := Sqrt(-1)
|
f, err := Sqrt(-1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user