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

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
}
主站蜘蛛池模板: 商南县| 东丽区| 商城县| 德保县| 巴彦淖尔市| 东乡| 淅川县| 阿拉善左旗| 阿拉尔市| 绥芬河市| 合水县| 连山| 大冶市| 延川县| 墨江| 枣强县| 嵩明县| 泰州市| 南皮县| 潜江市| 简阳市| 丰顺县| 望都县| 淳安县| 台江县| 岢岚县| 将乐县| 始兴县| 宁国市| 金门县| 南乐县| 介休市| 黔西县| 天全县| 山东| 林州市| 柘荣县| 浦北县| 台东市| 江都市| 长宁区|