Golang Case

func TestGolang(t *testing.T) {
fmt.Println(A()) // 0
fmt.Println(*B()) // 1
fmt.Println(C()) // 1
fmt.Println(D()) // 0
}

func A() int {
var result int
defer func() {
result++
}()
return result
}

func B() *int {
var result int
defer func() {
result++
}()
return &result
}

func C() (result int) {
defer func() {
result++
}()
return result
}

func D() int {
var result = 0
defer func() {
result++
}()
return result
}

 

posted @ 2020-01-14 15:46  小张的练习室  阅读(268)  评论(0编辑  收藏  举报