"Go" error handling

Source: Internet
Author: User



· The error type is an interface type and is a built-in type of go language. Only one method error is included in the declaration of this interface type. This method does not accept any arguments, but returns a string of results. Its function is to return an error message to the string representation of the image. The way we use the error type is typically to declare a result of that type at the end of the function declaration result list. At the same time, after calling this function, first determine whether it returns the last result is "not nil". If it is not nil then go to the error-handling process, or continue to the normal process.


Package main

Import (
     "errors"
     "fmt"
)

Func echo(request string) (response string, err error) {
     If request == "" {
         / / If the request is empty, call the error.New function to assign a value to the result, and then ignore the operation back. Response returns an empty string
         Err = errors.New("empty content")
         Return
     }
     Response = fmt.Sprintf("echo: %s", request)
     Return //err value is nil
}

Func main() {
     For _, req := range []string{"", "hello!"} {
         fmt.Printf("request: %s\n", req)
         Resp, err := echo(req)
         If err != nil {
             fmt.Printf("error: %s\n", err)
             Continue
         }
         fmt.Printf("response: %s\n", resp)
     }
}



Because error is an interface type, the actual type of error values may be different, even if they are of the same type. How do you determine what kind of error the error value represents?



1) for a series of error values of a type within a known range, it is common to use type assertion expressions or type switch statements to determine



2) for a series of error values that have the same type as the corresponding variable, it is generally judged by the operation of the sentence.



3) for a series of error values that do not have a response variable and whose type is unknown, only the string representation of its error message can be used to determine


Func underlyingError(err error) error {
        //Get and return potential error values for known operating system related errors
     Switch err := err.(type) {
     Case *os.PathError:
         Return err.Err / / return the Err field of the function parameter err
     Case *os.LinkError:
         Return err.Err
     Case *os.SyscallError:
         Return err.Err
     Case *exec.Error:
         Return err.Err
     }
     Return err / / directly return the function parameter value
}

printError := func(i int, err error) {
     If err == nil {
         fmt.Println("nil error")
         Return
     }
     Err = underlyingError(err)
     Switch err {
     Case os.ErrClosed:
         fmt.Printf("error(closed)[%d]: %s\n", i, err)
     Case os.ErrInvalid:
         fmt.Printf("error(invalid)[%d]: %s\n", i, err)
     Case os.ErrPermission:
         fmt.Printf("error(permission)[%d]: %s\n", i, err)
     }
}



"Go" error handling


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.