defer后面跟普通函数

  1. package main
  2. func main() {
  3. run()
  4. }
  5. func run() {
  6. name := "Paul"
  7. defer sayHello(name)
  8. name = "John"
  9. }
  10. func sayHello(name string) {
  11. println("Hello " + name)
  12. }
  13. // Hello Paul 只是延缓执行,实参会按照正常顺序传入函数中

defer,return和未命名的返回值

  1. package main
  2. func main() {
  3. name := run()
  4. println("return: name = " + name)
  5. }
  6. func run() (string) {
  7. name := "Paul"
  8. defer sayHello(&name)
  9. name = "John"
  10. return name
  11. }
  12. func sayHello(name *string) {
  13. *name = "George"
  14. println("Hello " + *name)
  15. }
  16. //Hello John 虽然实参提前传入,但是传入的是一个指针,最后执行拿到的是经过修改过的值
  17. //Hello George
  18. //return: name = John 未命名返回值,所以 defer 无法对返回的值进行操作的

defer、return 和命名的返回值

  1. package main
  2. func main() {
  3. name := run()
  4. println("return: name = " + name)
  5. func run() (x string) {
  6. name := "Paul"
  7. x = name
  8. defer sayHello(&x)
  9. name = "John"
  10. return name
  11. }
  12. func sayHello(name *string) {
  13. println("Hello " + *name)
  14. *name = "George"
  15. println("Hello " + *name)
  16. }
  17. //Hello John
  18. //Hello George
  19. //return: name = George 因为使用命名返回值,所 defer 里面依然可以对 defer 进行操作
  1. package main
  2. func main() {
  3. name := run()
  4. println("return: name = " + name)
  5. func run() (x string) {
  6. name := "Paul"
  7. x = name
  8. defer sayHello(x)
  9. name = "John"
  10. return name
  11. }
  12. func sayHello(name string) {
  13. println("Hello " + name)
  14. name = "George"
  15. println("Hello " + name)
  16. }
  17. //Hello Paul 延迟执行但是按照程序顺序执行,传入的实参为 Paul
  18. //Hello George 函数内修改对外面的值没有影响
  19. //return: name = John

defer 闭包函数

  1. package main
  2. func main() {
  3. name := run()
  4. println("return: name = " + name)
  5. }
  6. func run() (string) {
  7. name := "Paul"
  8. aFun := func() {
  9. println("Hello " + name)
  10. name = "George"
  11. println("Hello " + name)
  12. }
  13. name = "John"
  14. aFun()
  15. return name
  16. }
  17. //Hello John 虽然闭包定义时,外部变量 name = "Paul", 但是其实只是记录是一个指针,所以最后执行的时候 name = "John"
  18. //Hello George
  19. //return: name = George
  1. package main
  2. func main() {
  3. name := run()
  4. println("return: name = " + name)
  5. }
  6. func run() (x string) {
  7. name := "Paul"
  8. aFun := func() {
  9. println("Hello " + x)
  10. x = "George"
  11. println("Hello " + x)
  12. }
  13. defer aFun()
  14. name = "John"
  15. return name
  16. }
  17. //Hello John
  18. //Hello George
  19. //return: name = George defer 对返回值进行了修改
  1. package main
  2. func main() {
  3. name := run()
  4. println("return: name = " + name)
  5. }
  6. func run() (x string) {
  7. name := "Paul"
  8. x = name
  9. aFun := func(x string) {
  10. println("Hello " + x)
  11. x = "George"
  12. println("Hello " + x)
  13. }
  14. defer aFun(x)
  15. name = "John"
  16. return name
  17. }
  18. //Hello Paul 实参按照顺序拷贝传入 x = "Paul"
  19. //Hello George
  20. //return: name = John 因为设置了形参所以闭包内的对应的变量只是在局部生效,不会影响外部变量

image.jpeg