Home > Article > Backend Development > Various uses of the defer keyword in Golang functions
Golang is a very popular programming language with very rich language features, one of which is the use of the defer keyword to complete some specific functions. In this article, we will introduce various ways to use the defer keyword.
In Golang, the most commonly used function of the defer keyword is to delay the execution of the function. This means that after the function is executed, the function wrapped by the defer keyword will be delayed.
For example, we can use the defer keyword to print the log:
func foo() { defer log.Printf("Exiting foo()") // Do something ... }
In this way, when the function completes execution, the log will be printed.
In Golang, the execution of delayed functions can also be used to release certain resources, such as open files, network connections, etc.
For example, we can use the defer keyword to close a file:
func foo() { file, err := os.Open("test.txt") if err != nil { log.Fatal(err) } defer file.Close() // Do something ... }
In this example code, when the function completes execution, the file handle will be automatically closed.
Similarly, we can also use the defer keyword to close a database connection:
func foo() { db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/test") if err != nil { log.Fatal(err) } defer db.Close() // Do something ... }
In this sample code, when the function completes execution, the database connection will be automatically closed.
In Golang, we can use the defer keyword to delay error processing. This way, even if an error occurs in the function, we can guarantee that the delayed function will be executed.
For example, we can use the defer keyword to handle a file reading failure error:
func foo() (err error) { file, err := os.Open("test.txt") if err != nil { return err } defer func() { if closeErr := file.Close(); closeErr != nil { err = closeErr } }() // Do something ... return err }
In this sample code, if the file fails to open, an error is returned directly; otherwise, we use The defer keyword delays the closing of the file handle. At the same time, we checked whether there is an error in the closing operation in the defer function. If there is, both the closing error and the original error will be returned.
In Golang, we can use the defer keyword to recover certain exceptions.
For example, we can use the defer keyword to recover an erroneous function call:
func foo() (err error) { defer func() { if exception := recover(); exception != nil { err = fmt.Errorf("Caught an exception: %v", exception) } }() // Do something ... return err }
In this example code, we wrap the recovery function in a defer function. If an exception occurs during function execution, we will recover the exception and print out the exception information.
In short, the defer keyword in Golang is a very powerful function that can be used to complete a variety of tasks, including delayed function execution, delayed resource release, delayed error handling, and delayed recovery. Using the defer keyword can make our code more robust and clear.
The above is the detailed content of Various uses of the defer keyword in Golang functions. For more information, please follow other related articles on the PHP Chinese website!