func f1() (r int) { defer func() { r++ }() return 0 } func f2() (r int) { t := 5 defer func() { t = t + 5 }() return t } func f3() (r int) { defer func(r int) { r = r + 5 }(r) return 1 }
as:
f1() =1,return 把r设成0,然后defer把r改为1 ;
f2() =5,return 把r设成5,然后defer改的是t,不影响返回值 ;
f3() =5,return 把r设成1,然后defer把r改为r+5,但是用的r是defer设定时的r,=0;
(靠,是1,r+5的r不是外面的r)
标签:f1,f2,f3,return,defer,func From: https://www.cnblogs.com/gongxianjin/p/17239958.html