「Go言語」ラベル付きbreakを使うサンプル

書式
break ラベル名
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package main
import "fmt"
func main() {
fmt.Println("---- break label ----")
re:
for i := 5; i <= 9; i++ {
fmt.Printf("i: %d\n", i)
for m := 12; m <= 16; m++ {
fmt.Printf("m: %d\n", m)
break re
}
}
}
package main import "fmt" func main() { fmt.Println("---- break label ----") re: for i := 5; i <= 9; i++ { fmt.Printf("i: %d\n", i) for m := 12; m <= 16; m++ { fmt.Printf("m: %d\n", m) break re } } }
package main

import "fmt"

func main() {
fmt.Println("---- break label ----")
    re:
        for i := 5; i <= 9; i++ {
            fmt.Printf("i: %d\n", i)
            for m := 12; m <= 16; m++ {
                fmt.Printf("m: %d\n", m)
                break re
            }
        }
}

実行結果
>go run test.go
—- break label —-
i: 5
m: 12

Go言語

Posted by arkgame