mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 03:55:28 +08:00
39 lines
943 B
Go
39 lines
943 B
Go
// defer_dbconn.go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
doDBOperations()
|
|
}
|
|
|
|
func connectToDB () {
|
|
fmt.Println( "ok, connected to db" )
|
|
}
|
|
|
|
func disconnectFromDB () {
|
|
fmt.Println( "ok, disconnected from db" )
|
|
}
|
|
|
|
func doDBOperations() {
|
|
connectToDB()
|
|
fmt.Println("Defering the database disconnect.")
|
|
defer disconnectFromDB() //function called here with defer
|
|
fmt.Println("Doing some DB operations ...")
|
|
fmt.Println("Oops! some crash or network error ...")
|
|
fmt.Println("Returning from function here!")
|
|
return //terminate the program
|
|
// deferred function executed here just before actually returning, even if there is a return or abnormal termination before
|
|
}
|
|
/* Output:
|
|
ok, connected to db
|
|
Defering the database disconnect.
|
|
Doing some DB operations ...
|
|
Oops! some crash or network error ...
|
|
Returning from function here!
|
|
ok, disconnected from db
|
|
*/
|
|
|
|
|
|
|