//基础款,声明变量赋予好值,再进行if判断 if first <= 0 { fmt.Printf("first is less than or equal to 0\n") } else if first > 0 && first < 5 { fmt.Printf("first is between 0 and 5\n") } else { fmt.Printf("first is 5 or greater\n") } //声明变量,在if判断中,先赋值再判断。这个cond赋予的值会覆盖上面的声明变量。 if cond = 5; cond > 10 { fmt.Printf("cond is greater than 10\n") } else { fmt.Printf("cond is not greater than 10\n") } fmt.Println(cond) //等于5 //在if判断中,声明变量赋值再判断。 if cond2 := 5; cond2 > 10 { fmt.Printf("cond2 is greater than 10\n") } else { fmt.Printf("cond2 is not greater than 10\n") } fmt.Println(cond2) //报错,未声明cond2。因为if中声明的变量的作用域只存在于if结构中。 }