defer
package main
import (
"fmt"
)
func main() {
defer_call()
}
func defer_call() {
defer func() { fmt.Println("1") }()
defer func() { fmt.Println("2") }()
defer func() { fmt.Println("3") }()
panic("4")
}
Answer
Try it
3
2
1
panic: 4
defer1
package main
import (
"fmt"
)
type Person struct {
age int
}
func main() {
person := &Person{28}
defer fmt.Println(person.age)
defer func(p *Person) {
fmt.Println(p.age)
}(person)
defer func() {
fmt.Println(person.age)
}()
person.age = 29
}
Answer
Try it
29
29
28
defer2
package main
import (
"fmt"
)
type Person struct {
age int
}
func main() {
person := &Person{28}
defer fmt.Println(person.age)
defer func(p *Person) {
fmt.Println(p.age)
}(person)
defer func() {
fmt.Println(person.age)
}()
person = &Person{29}
}
Answer
Try it
29
28
28
defer3
package main
import (
"fmt"
)
var a bool = true
func main() {
defer func() {
fmt.Println("1")
}()
if a == true {
fmt.Println("2")
return
}
defer func() {
fmt.Println("3")
}()
}
Answer
Try it
2
1
defer4
package main
import "fmt"
type temp struct{}
func (t *temp) Add(elem int) *temp {
fmt.Print(elem)
return &temp{}
}
func main() {
tt := &temp{}
defer tt.Add(1).Add(2)
tt.Add(3)
}
- A. 132
- B. 123
- C. 312
- D. 321
Answer
Try it
A