对一段有关Go Code Block和变量作用域的代码的简要分析 原创 Tony Bai TonyBai 2018-05-11

对一段有关Go Code Block和变量作用域的代码的简要分析

 

https://mp.weixin.qq.com/s/vD8jOjkt_kBc9fd8huIUAQ

Each file has a file block containing all Go source text in that file.

4. Each "if", "for", and "switch" statement is considered to be in its own implicit block.

5. Each clause in a "switch" or "select" statement acts as an implicit block.


func writeDb(Type int) bool {
var B bool
switch Type {
case 1:
B = true

}
return B
}

type T struct {
B bool
}

func writeDb1(Type int) (t T) {
var B bool
switch Type {
case 1:
B = true
}
t.B = B
return
}

func main() {

i := writeDb(1)


j := writeDb(2)

i1 := writeDb1(1)


j1 := writeDb1(2)

fmt.Println(i,j,i1,j1)

原文地址:https://www.cnblogs.com/rsapaper/p/14542827.html