Go-goto-statement

提供:Dev Guides
移動先:案内検索

Go-gotoステートメント

Goプログラミング言語の goto ステートメントは、同じ関数内でgotoからラベル付きステートメントへの無条件ジャンプを提供します。

-プログラムの制御フローをトレースすることが難しくなり、プログラムが理解しにくく、変更しにくくなるため、どのプログラミング言語でも goto ステートメントを使用しないことを強くお勧めします。 gotoを使用するプログラムは、他の構造を使用して書き換えることができます。

構文

Goの goto ステートメントの構文は次のとおりです-

goto label;
..
.
label: statement;

ここで、 label はGoキーワードを除く任意のプレーンテキストにすることができ、Goプログラムの上下の goto ステートメントに設定できます。

流れ図

Go Gotoステートメント

package main

import "fmt"

func main() {
  /*local variable definition*/
   var a int = 10

  /*do loop execution*/
   LOOP: for a < 20 {
      if a == 15 {
        /*skip the iteration*/
         a = a + 1
         goto LOOP
      }
      fmt.Printf("value of a: %d\n", a)
      a++
   }
}

上記のコードをコンパイルして実行すると、次の結果が生成されます-

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19