defer关键字通常用于清洁目的。 defer关键字将函数或语句的执行推迟到调用函数的结尾。
当封闭函数在右花括号}之前返回时, 它将执行代码(函数或表达式)。如果在执行封闭功能期间发生错误, 也将执行该命令。
defer示例
package main
import (
"fmt"
)
func main() {
defer print1("Hi...")
print2("there")
}
func print1(s string) {
fmt.Println(s)
}
func print2(s string) {
fmt.Println(s)
}
输出:
there
Hi...
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_50059.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57