golang if作用域
package main import ( "fmt" ) func main() { x := 0 if x := test(); x < 0 { fmt.Println("first") fmt.Println(x) } else { fmt.Println("second") fmt.Println(x) } fmt.Println(x) } func test() int { return 1 }
结果:
second
1
0
package main import ( "fmt" ) func main() { x := 0 if x := test(); x < 0 { fmt.Println("first") fmt.Println(x) } else { fmt.Println("second") fmt.Println(x) } fmt.Println(x) } func test() int { return 1 }
结果: