官术网_书友最值得收藏!

if

The if statement comes with the if, else if, and else clauses, just like most other languages. The one interesting feature that Go has is the ability to put a statement before the condition, creating temporary variables that are discarded after the if statement has completed.

This example demonstrates the various ways to use an if statement:

package main

import (
"fmt"
"math/rand"
)

func main() {
x := rand.Int()

if x < 100 {
fmt.Println("x is less than 100.")
}

if x < 1000 {
fmt.Println("x is less than 1000.")
} else if x < 10000 {
fmt.Println("x is less than 10,000.")
} else {
fmt.Println("x is greater than 10,000")
}

fmt.Println("x:", x)

// You can put a statement before the condition // The variable scope of n is limited
if n := rand.Int(); n > 1000 {
fmt.Println("n is greater than 1000.")
fmt.Println("n:", n)
} else {
fmt.Println("n is not greater than 1000.")
fmt.Println("n:", n)
}
// n is no longer available past the if statement
}
主站蜘蛛池模板: 五大连池市| 广汉市| 榆社县| 宁安市| 伊宁市| 达州市| 万载县| 略阳县| 饶平县| 大洼县| 黑水县| 邯郸市| 山东省| 高雄市| 灌云县| 资溪县| 沙坪坝区| 安溪县| 香格里拉县| 嘉荫县| 宁远县| 岗巴县| 岱山县| 安阳市| 马关县| 陆河县| 珠海市| 瓮安县| 裕民县| 酉阳| 襄樊市| 盐源县| 济源市| 海安县| 栖霞市| 饶阳县| 天镇县| 博乐市| 天峨县| 泰安市| 洪湖市|