First knowledge Go (4)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
There is a good design in the Defergo language, the deferred (defer) statement, in which you can add multiple defer statements to the 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") defer file. Close () If Failurex {return false}if Failurey {return False}return true} function as a value, type is a variable in Go, and we can define it by type, which is all The same parameter, the same as the return value of type TypeName func (input1 inputType1 [, Input2 inputType2 [, ...]) (Result1 resultType1 [, ...]) Type testint func (int) bool//declares a function type func isodd (integer int) bool {if integer%2 = = 0 {return False}return true}func fil ter (Slice []int, F testint) []int {var result []intfor _, Value: = Range Slice {if f (value) {result = Append (result, value )}}return Result}odd: = Filter (slice, isodd)//pass in function parameters panic and Recovergo there is no exception mechanism like Java, it cannot throw an exception, but uses the panic and recover mechanism of your code There should be no, or little panic, of things. Panic: is a built-in function that interrupts the original control flow and enters a scary process. When function f calls panic, the execution of the function f is interrupted, but the delay function in F executes normally, and then F returns to the place where it was called. Where the call is made, the behavior of F is like calling panic. This process continues upward until all called functions in the goroutine that occur panic are returned, at which point the program exits. Panic can be directly adjustedProduced with panic. It can also be generated by a run-time error, such as an array that accesses out of bounds. Recover: is a built-in function that allows Goroutine to recover from a panic-entering process. The recover is only valid in the Delay function (defer). During normal execution, the call to recover returns nil and has no other effect. If the current goroutine is in panic, call recover can capture the input value of panic and return to normal execution.

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.