Defer
คือการสั่งให้ Function || Method นั้นๆ มีการทำงานหลังจากที่ Function main ทำงานเสร็จ หรืออีกความหมายคือ การสั่งให้ Function นั้นๆ ทำงานหลังสุดก่อนที่โปรแกรมจะปิดpackage main
import (
"fmt"
)
func main() {
fmt.Println("===== Start =====")
name := "AA"
hello(name)
fmt.Println("===== End =====")
}
func hello(name string) {
fmt.Printf("name => %s \n", name)
}
$ go run .
...
===== Start =====
name => AA
===== End =====
defer
package main
import (
"fmt"
)
func main() {
fmt.Println("===== Start =====")
name := "AA"
defer hello(name)
fmt.Println("===== End =====")
}
func hello(name string) {
fmt.Printf("name => %s \n", name)
}
$ go run .
...
===== Start =====
===== End =====
name => AA
package main
import (
"fmt"
)
func main() {
fmt.Println("===== Start =====")
name := "AA"
hello1(name)
hello2(name)
hello3(name)
fmt.Println("===== End =====")
}
func hello1(name string) {
fmt.Printf("name1 => %s \n", name)
}
func hello2(name string) {
fmt.Printf("name2 => %s \n", name)
}
func hello3(name string) {
fmt.Printf("name3 => %s \n", name)
}
$ go run .
...
===== Start =====
name1 => AA
name2 => AA
name3 => AA
===== End =====
Go โดยปกติจะมีการทำงานแบบจากบนลงล่างไปเรื่อยๆ
package main
import (
"fmt"
)
func main() {
fmt.Println("===== Start =====")
name := "AA"
defer hello1(name)
defer hello2(name)
defer hello3(name)
fmt.Println("===== End =====")
}
func hello1(name string) {
fmt.Printf("name1 => %s \n", name)
}
func hello2(name string) {
fmt.Printf("name2 => %s \n", name)
}
func hello3(name string) {
fmt.Printf("name3 => %s \n", name)
}
$ go run .
...
===== Start =====
===== End =====
name3 => AA
name2 => AA
name1 => AA
การใช้ defer ลำดับมันก็จะตรงกันข้ามกับการทำงานปกติคือ จะทำงานจากตัวที่ใส่ defer ล่างสุดขึ้นไปข้างบน
package main
import (
"fmt"
)
func main() {
name := "AA"
fmt.Println("===== Start =====")
hello1(name)
hello2(name)
hello3(name)
fmt.Println("===== End =====")
defer fmt.Println("===== Start =====")
defer hello1(name)
defer hello2(name)
defer hello3(name)
defer fmt.Println("===== End =====")
}
func hello1(name string) {
fmt.Printf("name1 => %s \n", name)
}
func hello2(name string) {
fmt.Printf("name2 => %s \n", name)
}
func hello3(name string) {
fmt.Printf("name3 => %s \n", name)
}
$ go run .
...
===== Start =====
name1 => AA
name2 => AA
name3 => AA
===== End =====
===== End =====
name3 => AA
name2 => AA
name1 => AA
===== Start =====