Go language Learning (vii) Closures and error handling

Source: Internet
Author: User
Tags anonymous closure error handling printf
1. Closures
Closures in the Go language also refer to variables outside the function. The closure implementation ensures that as long as the closure is used,
the variables referenced by the closure will persist, for example:
Package main

Import "FMT"

func Main () {
    var J int = 5
    A: = Func () (func ()) {//parentheses () func () means the return value is a func () function C7/>var I int = ten
        return func () {//Here returns an anonymous function
            FMT. Printf ("I, J:%v,%v\n", I, J)
        }
    } ()//curly braces followed by a parameter list indicates that an anonymous function is called, and the variable a here is equal to a function.

    A ()//Call function a

    J *= 2//Modify the variable outside the function J

    A ()//Call function a
} again
Operating results:
I, j:10, 5
I, J:10, 10
In the above example, the closure function that the variable a points to refers to the local variables I and J, the value of I is isolated, can not be modified outside the closure,
after changing the value of J, call a again, found that the result is a modified value. In the
closure function that the variable a points to, only the internal anonymous function can access the variable I and cannot be accessed by other means
, thus guaranteeing the security of I.
2. Error handling

2.1 Error interface

The go language introduces a standard pattern of error handling, the error interface, which is defined as follows:
type error Interface {
    error () string
}
creation error is usually as follows:
var e error = errors. New ("...")//need to use the errors package

for most functions, if you want to return an error, you can roughly define it as the following pattern, using error as the last of several return
values, but this is not a mandatory requirement:
Func Foo (param int) (res int,err error) {
    //...
}
The code at the time of invocation recommends handling error conditions as follows:
N, Err: = Foo (0)
if err! = Nil {
    //error handling
} else {
    //Use return value N
}

2.2 Defer keywords

There is a good design in the go language, the deferred (defer) statement, where you can add multiple defer statements to a function. When the function executes to the last,
these defer statements are executed in reverse order, and finally the function returns. Especially when you are doing some open resources operation, encountered errors need to return early,
before returning you need to close the corresponding resources, otherwise it is easy to cause problems such as resource leakage.
Func ReadWrite () bool {
    file. Open ("file")
    //Do some work
    if Failurex {
        file. Close ()
        return False
    }
    if Failurey {
        file. Close ()
        return False
    }
    file. Close ()
    return True
}
We see a lot of duplicated code on it, and the go defer effectively solves this problem. With it, not only is the amount of code reduced a lot,
but the program becomes more elegant.
Func ReadWrite () bool {
    file. Open ("file")
    defer file. Close ()//Ensure that the resource is properly closed
    if Failurex {
        return false
    }
    if Failurey {
        return false
    }
    return true< c11/>}
If there are many calls to defer, then defer is in LIFO mode
For I: = 0; I < 5; i++ {
    defer FMT. Printf ("%d", i)//output Result: 4 3 2 1 0
}
Defer a bit like the try{}finall{in Java}

2.3 Panic and recover functions

The go language has 2 built-in functions panic () and recover () to report and capture program errors that occur at run time, unlike error,
where panic and recover are generally used inside functions. Be careful not to misuse panic and recover, which can lead to performance problems and are
generally only used when unknown input and unreliable requests.

Go language error handling process: When a function occurs during execution of an exception or encounters panic (), the normal statement terminates immediately,
then executes the defer statement, reports the exception information, and finally exits the goroutine. If the recover () function is used in defer, an
error message is captured that causes the error message to terminate the report. The example below is from the network
Package main
Import (
 "log"  //log packet
 "StrConv"//Character conversion package
)
//Capture program exception caused by unknown input
func catch ( nums. int) int {
    defer func () {
        //recover () can catch the exception that occurs at run time, avoid the exception when the program directly over, usually used in the defer function
        if r: = Recover (); R! = Nil { 
            log. Println ("[E]", r)//to print the captured exception information through log without causing the program to hang Up
        }
    } ()
    return nums[1] * nums[2] * nums[3]//index out of rang E
}
//Active throw panic, deprecated, may cause performance problems with
func toFloat64 (num string) (float64, error) {
    defer func () {
        if r: = Recover (); r! = nil { 
            log. Println ("[W]", R)
        }
    } ()
    if num = = "" {
        panic ("param is null")//actively throws panic
    }
    return StrConv. Parsefloat (num, ten) 
}

func main () {
    catch (2, 8)
    ToFloat64 ("")
}
Run Result:
2016/03/26 20:16:03 [E] Runtime error:index out of range
2016/03/26 20:16:03 [W] param is null

Finally, add nil to the introduction:

The nil of 
 Golang, which is conceptually the same as null, None, nil, and Null in other languages, refers to a value of 0 or null. Nil is a pre-stated identifier, also a keyword in the usual sense. In Golang, nil can only be assigned to variables of the pointer, channel, Func, interface, map, or slice type.
If this rule is not followed, panic is raised. 
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.