📕
innohub
  • KEEP LEARNING
  • WebInfo
    • JS 部分运算符操作
    • javascript 中的object array
    • 错误处理以及异常捕获
    • JavaScript Bases
    • LaoZi & Confucius
  • PyInfo
    • Python3 输入与输出
    • Python3OS
    • python 修饰器的使用
    • python3 与mysql数据库连接使用实例
    • Format-specifier
    • CGI初学踩坑
    • Django 项目测试
    • Assert-info
    • 使用ngnix和uWSGI配置Django
    • write|SVN​
    • Matplotlib 基本使用
    • 重读 Python 官方文档
    • Python3 Base
    • python3 多线程
    • Python3 函数解析
    • python3 str 对象的基本操作
    • protocl buffers
    • Iterator-Generator
    • Django base
    • Decorator 2
    • Export to wheel or egg-info
    • 三. 运算、数据及逻辑
  • GoInfo
    • Info
      • Select 语句使用
      • First class function
      • Work Pools
      • StructTag
      • Go struct
      • 常用函数
      • Strings操作
      • Golang Bases
      • 数组以及切片
      • 文件操作
      • Golang 中的指针类型
      • Golang Map 类型
      • Reflection
      • 函数闭包
      • 接口
      • Panic and Recover
      • Go中的错误处理
      • 并发
      • defer usage
      • Method in golang
      • Object-oriented Programming
      • Goalng 包管理机制
  • RustInfo
    • Info
      • 包、crate、模块
      • Rust 中的错误处理
      • 智能指针
      • 泛型 generics
      • 数据布局与内存对齐
      • Functions and methods
      • 无畏并发
      • Actix-web 基本使用
      • Got from guessing game
      • 结构体的使用
      • Rust 中的函数式语言功能
      • 集合类型
      • 枚举以及模式匹配
      • Slice 类型
      • 生命周期
      • 测试
      • Rust 中的所有权
      • trait
      • 不安全 Rust
      • Format_print
      • Rust 通用编程概念
      • Macro
  • OS
    • info
      • 内存屏障 -- Part 1
      • 内存屏障 -- Part 2
      • CPU 上下文切换
      • 文件读写与零拷贝
      • ELF 文件
  • MySql
    • info
      • MySql 架构与历史
      • 02-key
  • kubernetes
    • 第二章 k8s 基本概念
    • 第一章 Kubernetes -- 伟大舵手
  • Redis
    • info
      • Redis 总览
      • 02-underline-ds
  • Shell&CInfo
    • GCC 与make的使用
    • C++ 中 malloc 与 new
    • 位运算符操作
    • Base of C macro
    • C 语言中 extern 关键字的用法
  • Distributed
    • info
      • 分布式理论概述
  • Java
    • info
      • Java 内存模型
  • Prometheus
    • Prometheus -- 不灭之火
Powered by GitBook
On this page
  • 1. 基础用法
  • 1.1 参数的值
  • 1.2 defers stack
  • 2. defer的典型用法

Was this helpful?

  1. GoInfo
  2. Info

defer usage

[TOC]

1. 基础用法

defer语句主要用改变程序的执行流程,用于添加一个方法或者函数在一个方法执行结束返回之前调用defer指向的方法。

type person struct {
  name string
  age int
}

func (p person) display() {
  fmt.Println(p.name, p.age)
}

func defer_test() {
  p := person{"Leborn James", 36}
  defer p.display()
  p.age += 1
  fmt.Println("Let me intro to u:")
  fmt.Println("and after onr year his age:", p.age)
}

func main() {
  defer_test()
}
/*Let me intro to u:
and after onr year his age: 37
Leborn James 36
*/

如上述代码所示,在函数defer_test中使用defer语句,调用了person的一个方法,在该函数返回之前,调用了person的display方法。

1.1 参数的值

需要注意的是,如果defer语句使用的参数为变量时,那么参数的值绑定的是使用该语句时的值,如果变量值发生改变,参数仍保留原始值。

package main

import (  
    "fmt"
)

func printA(a int) {  
    fmt.Println("value of a in deferred function", a)
}
func main() {  
    a := 5
    defer printA(a)
    a = 10
    fmt.Println("value of a before deferred function call", a)

}

// value of a in deferred function 5  
//

使用defer语句后,参数保留的是此时a的值,之后改变了其值,但是该函数使用的是按值传递,。只是把变量a的值复制了一遍,所以改变了原始a的值不对函数调用产生影响。但是注意如果使用指针类型或者使用切片类型作为参数时,defer参数与main Function中的变量指向同一个地址。

重点在于参数与原始的变量是否指向同一个地址。

1.2 defers stack

当一个函数中使用多个defer调用,那么这些调用的执行顺序按照栈的形式,后进先出(LIFO)。也就是最后一个defer语句先执行,然后向上返回。所以一组defer调用也具备了栈的性质,所以可以实现倒序等操作。

func main() {  
    name := "Naveen"
    fmt.Printf("Orignal String: %s\n", string(name))
    fmt.Printf("Reversed String: ")
    for _, v := range []rune(name) {
        defer fmt.Printf("%c", v)
    }
}

2. defer的典型用法

defer语句的主要作用是使得代码会更加简洁明了,通过defer函数或者方法可以对控制流进行改变。典型用例是用在一个sync.WaitGroup对象的Done方法。

package main

/*Pratical usage cases of the defer statement */
import (
  "fmt"
  "sync"
)

type rect struct {
  length, width float64
}

func (r rect) area(wg *sync.WaitGroup) {
  defer wg.Done()
  if r.width <= 0 {
    fmt.Println("The width of rect must be positive")
    return
  }
  if r.length <= 0 {
    fmt.Println("The length of rect must be positive")
    return
  }
  area := r.length * r.width
  fmt.Println("The area of rect:", area)
}

func main() {
  r1 := rect{12.0, -9.2}
  r2 := rect{11.2, 2}
  r3 := rect{11., -90.}
  rects := []rect{r1, r2, r3}
  var wg sync.WaitGroup
  for _, item := range rects {
    wg.Add(1)
    go item.area(&wg)
  }
  wg.Wait()
  fm
    t.Println("All the routine finished")
}
/*The width of rect must be positive
The width of rect must be positive
The area of rect: 22.4
All the routine finished
*/

由于在进行条件判断时,每次在程序返回前都必须执行wg.Done()方法,通过使用defer语句不仅使得代码更加简洁,而且当有新的条件需要加入时,不需要担心是否添加了wg.Done()方法。

Previous并发NextMethod in golang

Last updated 5 years ago

Was this helpful?