"Go" error handling

Source: Internet
Author: User

Tags: operating system request UNC Perm Range ONS Statement declaration method

· 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 Mainimport ("Errors"    "FMT") func Echo (Requeststring) (responsestring, err Error) {    ifRequest = ="" {          //If request is empty, call error. The new function assigns a value to the result, and then ignores the back operation. Response returns an empty stringErr = 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)ifErr! =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 {//gets and returns potential error values for known operating system-related errors    SwitchERR: =Err. (type) {     Case*OS. Patherror:returnErr. Err//returns the Err field of the function parameter err     Case*OS. Linkerror:returnErr. ERR Case*OS. Syscallerror:returnErr. ERR Case*exec. Error:returnErr. ERR}returnErr//returns the function parameter value directly}printerror:= Func (iint, err Error) {    ifErr = =Nil {fmt. Println ("Nil Error")        return} Err=Underlyingerror (Err)SwitchErr { CaseOS. Errclosed:fmt. Printf ("error (closed) [%d]:%s\n", I, err) CaseOS. Errinvalid:fmt. Printf ("error (invalid) [%d]:%s\n", I, err) CaseOS. 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.

Tags Index: