Go language some discussions and personal views on Go error handling style (bottom)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Original articles, reproduced please specify the Source: Server non-amateur research-sunface

It is critical and important for go to deal with errors, and here I summarize some ways to avoid error handling (see some discussion and personal opinions on Go error handling style).

1. Error code common style:

The first example of the following two examples is significantly better than the second one

F, err: = OS. Open (PATH)
If err! = Nil {
Handle error
}
Do stuff

F, err: = OS. Open (PATH)
If Err = = Nil {
Do stuff
}
Handle error

2. Custom error handling Style:

First define the error interface:

Type Error string

Func (e Error) error () string {return string (E)}

Then use type assertion to determine the type of error

result, err: = Yourpackage. Foo ()
If ype, OK: = Err. (Yourpackage. ERROR); OK {
Use ype to handle error
}

Parse the file and output the error message:

Type Openerror struct {
File *file
Error string
}

Func (OE *openerror) Error () string {
Format error string here
}

Func parsefiles (Files []*file) error {
For _, F: = Range Files {
ERR: = F.parse ()
If err! = Nil {
Return &openerror{
File:f,
Error:err. Error (),
}
}
}
}

This will tell you which file has failed in the analysis.

One thing to pay special attention to: encapsulation error, because when you encapsulate the error, the original error message may be lost:

var c net. Conn
F, err: = DownloadFile (c, Path)
Switch e: = Err. (type) {
Case Net. Error:
Close connection

C.close ()
Return E
Case ERROR:
Error! = Nil
return err

Default
Err = = Nil will be executed here.

}
Do other things.

If you encapsulate net in this piece of code. Error, this code will no longer be considered a net error, will not close the connection, continue to use the wrong connection. A better approach is: when using external interfaces, do not encapsulate the errors they produce!!

3. The error is the same as the state:

Sometimes you will not deal with a mistake for the time being, perhaps because you want to postpone processing, or because the error will appear again soon.

For the first case, Bufio is a good example when Bufio. When reader encounters an error, it will hold the error until the cache is empty to handle the error.

For the second case, Go/loader is a good example, and when it is called and an error occurs, it holds that error because it is likely that it will be called again with considerable parameters.

4. Use functions to avoid duplication

If an error occurs repeatedly, you can use a function to simplify it:

Func HandleError (c net. Conn, err Error) {
Perform error post-processing
}

Func Dostuff (c net. Conn) Error {
F, err: = DownloadFile (c, Path)
If err! = Nil {
HandleError (c, Err)
return err
}

F, err: = Dootherthing (c)
If err! = Nil {
HandleError (c, Err)
return err
}
}

Or you can use the following style:

Func HandleError (c net. Conn, err Error) {
If Err = = Nil {
Return
}
Error post-processing

}

Func Dostuff (c net. Conn) Error {
Defer func () {HandleError (c, err)} ()
F, err: = DownloadFile (c, Path)
If err! = Nil {
return err
}

F, err: = Dootherthing (c)
If err! = Nil {
return err
}
}

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.