記事ディレクトリ
フロー制御ステートメント
もし
if 5 > 9 {
fmt.Println("5>9")
}
- 論理式が true の場合、{} 内の内容が実行されます。
- 論理式を追加する必要はありません ()。
- 「{」は論理式の直後に続く必要があり、新しい行で始めることはできません。
if c, d, e := 5, 9, 2; c < d && (c > e || c > 3) {
//初始化多个局部变量。复杂的逻辑表达式
fmt.Println("fit")
}
- 論理式には変数または定数を含めることができます。
- if ステートメントにはセミコロンを 1 つ (1 つだけ) 含めることができ、一部のローカル変数はセミコロンの前で初期化されます (つまり、ローカル変数は if ブロック内でのみ表示されます)。
if-elseの使用法
color := "black"
if color == "red" {
//if只能有一个
fmt.Println("stop")
} else if color == "green" {
fmt.Println("go")
} else if color == "yellow" {
//else if可以有0个、一个或者连续多个
fmt.Println("stop")
} else {
//else有0个或1个
fmt.Printf("invalid traffic signal: %s\n", strings.ToUpper(color))
}
ネストされた if 式
if xxx {
if xxx {
}else if xxx{
}else{
}
}else{
if xxx {
}else{
}
}
ネストが深すぎると、コードのメンテナンスが困難になることに注意してください。
if (true) {
if (true) {
if (true) {
if (true) {
if (true) {
}
}
}
}
}
スイッチ
color := "black"
switch color {
case "green" : //相当于 if color== "green"
fmt.Println("go")
case "red" : //相当于else if color== "red"
fmt.Println("stop")
default: //相当于else
fmt.Printf("invalid traffic signal: %s\n", strings.ToUpper(color))
}
- switch-case-default は if-else if-else をシミュレートできますが、実現できるのは等しいかどうかの判断のみです。
- 同じデータ型を表す限り、switch と case の後に定数、変数、または関数式を続けることができます。
- 1 つの値がそれを満たす限り、case の後に複数の値を続けることができます。
func add(a int) int {
return a + 10
}
func switch_expression() {
var a int = 5
switch add(a) {
//switch后跟一个函数表达式
case 15: //case后跟一个常量
fmt.Println("right")
default:
fmt.Println("wrong")
}
const B = 15
switch B {
//switch后跟一个常量
case add(a): //case后跟一个函数表达式
fmt.Println("right")
default:
fmt.Println("wrong")
}
}
switch の後に式がある場合、switch-case は等しい状況のみをシミュレートできます。switch の後に式がない場合、case の後に任意の条件式を続けることができます。
func switch_condition() {
color := "yellow"
switch color {
case "green":
fmt.Println("go")
case "red", "yellow": //用逗号分隔多个condition,它们之间是“或”的关系,只需要有一个condition满足就行
fmt.Println("stop")
}
//switch后带表达式时,switch-case只能模拟相等的情况;如果switch后不带表达式,case后就可以跟任意的条件表达式
switch {
case add(5) > 10:
fmt.Println("right")
default:
fmt.Println("wrong")
}
}
スイッチの種類
func switch_type() {
var num interface{
} = 6.5
switch num.(type) {
//获取interface的具体类型。.(type)只能用在switch后面
case int:
fmt.Println("int")
case float32:
fmt.Println("float32")
case float64:
fmt.Println("float64")
case byte:
fmt.Println("byte")
default:
fmt.Println("neither")
}
switch value := num.(type) {
//相当于在每个case内部申明了一个变量value
case int: //value已被转换为int类型
fmt.Printf("number is int %d\n", value)
case float64: //value已被转换为float64类型
fmt.Printf("number is float64 %f\n", value)
case byte, string: //如果case后有多个类型,则value还是interface{}类型
fmt.Printf("number is inerface %v\n", value)
default:
fmt.Println("neither")
}
//等价形式
switch num.(type) {
case int:
value := num.(int)
fmt.Printf("number is int %d\n", value)
case float64:
value := num.(float64)
fmt.Printf("number is float64 %f\n", value)
case byte:
value := num.(byte)
fmt.Printf("number is byte %d\n", value)
default:
fmt.Println("neither")
}
}
fallthrough とは、あるケースにヒットした場合に、強制的に次のケースに入る機能です。
func fall_throth(age int) {
fmt.Printf("您的年龄是%d, 您可以:\n", age)
switch {
case age > 50:
fmt.Println("出任国家首脑")
fallthrough
case age > 25:
fmt.Println("生育子女")
fallthrough
case age > 22:
fmt.Println("结婚")
fallthrough
case age > 38:
fmt.Println("开车")
fallthrough
case age > 16:
fmt.Println("参加工作")
case age > 15:
fmt.Println("上高中")
fallthrough
case age > 3:
fmt.Println("上幼儿园")
}
}
のために
arr := []int{
1, 2, 3, 4, 5}
for i := 0; i < len(arr); i++ {
//正序遍历切片
fmt.Printf("%d: %d\n", i, arr[i])
}
for はローカル変数、条件式、後続の操作を初期化します。
for sum, i := 0, 0; i < len(arr) && sum < 100; sum, i = sum*1, i+1
- ローカル変数は for ブロック内でのみ表示されます。
- 初期化変数は上に配置できます。
- 後続の操作は for ブロック内に配置できます。
- 条件判定のみが必要な場合は前後のセミコロンは不要です。
- for{} は無限ループです。
範囲用
- 配列またはスライスを反復処理する
- for i, ele := range arr
- トラバースストリング
- for i, ele := range "I can sing ABC" //ele はルーンタイプです
- マップを走査するとき、go は走査の順序を保証しません。
- キーの場合、値 := 範囲 m
- チャネルを横断します。横断する前に必ず閉じてください
- ele := 範囲 ch の場合
- range が取得するのはデータのコピーです
ネストされた
行列の乗算には、3 レベルの for ループのネストが必要です。
func nest_for() {
const SIZE = 4
A := [SIZE][SIZE]float64{
}
//初始化二维数组
//两层for循环嵌套
for i := 0; i < SIZE; i++ {
for j := 0; j < SIZE; j++ {
A[i][j] = rand.Float64() //[0,1)上的随机数
}
}
B := [SIZE][SIZE]float64{
}
for i := 0; i < SIZE; i++ {
for j := 0; j < SIZE; j++ {
B[i][j] = rand.Float64() //[0,1)上的随机数
}
}
rect := [SIZE][SIZE]float64{
}
//三层for循环嵌套
for i := 0; i < SIZE; i++ {
for j := 0; j < SIZE; j++ {
prod := 0.0
for k := 0; k < SIZE; k++ {
prod += A[i][k] * B[k][j]
}
rect[i][j] = prod
}
}
i, j := 2, 1
fmt.Println(A[i]) //二维数组第i行
//打印二维数组的第j列
//注意:B[:][j]这不是二维数组第j列,这是二维数组第j行!
for _, row := range B {
fmt.Printf("%g ", row[j])
}
fmt.Println()
fmt.Println(rect[i][j])
}
休憩と継続
- Break と continue は、for ループのコード フローを制御するために使用され、それ自体に最も近い外側の for ループのみを対象とします。
- Break: for ループを終了すると、このラウンドの Break 下のコードは実行されなくなります。
- continue: continue 以下のコードはこのラウンドでは実行されなくなり、for ループの次のラウンドに入ります。
//break和continue都是针对for循环的,不针对if或switch
//break和continue都是针对套在自己外面的最靠里的那层for循环,不针对更外层的for循环(除非使用Label)
func complex_break_continue() {
const SIZE = 5
arr := [SIZE][SIZE]int{
}
for i := 0; i < SIZE; i++ {
fmt.Printf("开始检查第%d行\n", i)
if i%2 == 1 {
for j := 0; j < SIZE; j++ {
fmt.Printf("开始检查第%d列\n", j)
if arr[i][j]%2 == 0 {
continue //针对第二层for循环
}
fmt.Printf("将要检查第%d列\n", j+1)
}
break //针对第一层for循环
}
}
}
goto与Label
var i int = 4
MY_LABEL:
i += 3
fmt.Println(i)
goto MY_LABEL //返回定义MY_LABEL的那一行,把代码再执行一遍(会进入一个无限循环)
if i%2 == 0 {
goto L1 //Label指示的是某一行代码,并没有圈定一个代码块,所以goto L1也会执行L2后的代码
} else {
goto L2//先使用Label
}
L1:
i += 3
L2: //后定义Label。Label定义后必须在代码的某个地方被使用
i *= 3
goto と Label を組み合わせると、break よりもさらに強力な Break の機能を実現できます。
for i := 0; i < SIZE; i++ {
L2:
for j := 0; j < SIZE; j++ {
goto L1
}
}
L1:
xxx
- Break、Continue、Label を組み合わせて使用すると、外側の for ループにジャンプできます。
- continue と Break の対象となるラベルは for の前に記述する必要がありますが、goto は任意の位置のラベルを対象にすることができます。
func break_label() {
const SIZE = 5
arr := [SIZE][SIZE]int{
}
L1:
for i := 0; i < SIZE; i++ {
L2:
fmt.Printf("开始检查第%d行\n", i)
if i%2 == 1 {
L3:
for j := 0; j < SIZE; j++ {
fmt.Printf("开始检查第%d列\n", j)
if arr[i][j]%3 == 0 {
break L1 //直接退出最外层的fot循环
} else if arr[i][j]%3 == 1 {
goto L2 //continue和break针对的Label必须写在for前面,而goto可以针对任意位置的Label
} else {
break L3
}
}
}
}
}