go defer 相关知识点 电脑版发表于:2022/2/18 22:16 ```go package main type Foo struct { v int } func NewFoo(n *int) Foo { println("NewFoo",*n) return Foo{} } func (Foo) Bar(n *int) { println("bar",*n) } func main() { var x = 1 var p = &x println("1---",*p) defer NewFoo(p).Bar(p) println("2---",*p) x = 2 p = new(int) println("start---",*p) NewFoo(p) println("end--",*p) } ``` result ``` 1--- 1 NewFoo 1 2--- 1 start--- 0 NewFoo 0 end-- 0 bar 2 ``` ![](https://img.tnblog.net/arcimg/17607487550/12ab37a5862948ce9dbe2d95cb3fd83e.png)