0%

Go_Learning_Fundamental_Label in Go Land

Labels in go is used in break and continue statement which is optional but it’s required in goto statement. The scope of Labels is the function where it’s declared.

Declare And Definition

A label is declared with format LabelName:, where the LabelName is an identifier. A label which name is not a blank identifier must be used at least once time.

Feature

  • A label must be declared in a function body.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package main

    import "fmt"

    func main() {
    i := 0

    Next: // here, a label is declared.
    fmt.Println(i)
    i++
    if i < 5 {
    goto Next // execution jumps
    }
    }
  • A label is not visible/available outside the innermost block which the label is declared.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package main

    func main() {
    goto Label1 // error
    {
    Label1:
    goto Label2 // error
    }
    {
    Label2:
    }
    }
  • If a label is declared with the scope of variable a, the use a label can’t appear before the declaration of variable a.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    package main

    import "fmt"

    func main() {
    i := 0
    Next:
    if i >= 5 {
    // error: jumps over declaration of k
    goto Exit
    }

    k := i + i
    fmt.Println(k)
    i++
    goto Next

    // This label is declared in the scope of k,
    // but its use is outside of the scope of k.
    Exit:
    }
    • Fixed way one: shrink the variable scope

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      func main() {
      i := 0
      Next:
      if i >= 5 {
      goto Exit
      }
      // Create an explicit code block to
      // shrink the scope of k.
      {
      k := i + i
      fmt.Println(k)
      }
      i++
      goto Next
      Exit:
      }
    • Fixed way two: enlarge the variable scope

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      func main() {
      var k int // move the declaration of k here.
      i := 0
      Next:
      if i >= 5 {
      goto Exit
      }

      k = i + i
      fmt.Println(k)
      i++
      goto Next
      Exit:
      }

      Usage

There are three control flow jump statements,goto, break, continue , could apply with label; The goto statement must follow with label while break, continue has option to choice.

goto Statement with label

The basic format of goto statement with label is :

1
2
3
4
5
6
goto label

do something

label:
statement

In the first part, the usage of goto has explained, in fact there is no much details could talk with goto. In practice goto is not recommend to use. It a easy way to deduct readability and product bug.

break Statement with label

Generally, break containing label is used in nested breakable control flow blocks.

If a break statement contains a label, the label must be declared just before a breakable control flow block which contains the break statement. We can view the label name as the name of the breakable control flow block. The break statement will make execution jump out of the breakable control flow block, even if the breakable control flow block is not the innermost breakable control flow block containing break statement.

continue Statement with label

Generally, continue containing label is used in nested breakable control flow blocks.

If a continue statement contains a label, the label must be declared just before a loop control flow block which contains the continue statement. We can view the label name as the name of the loop control flow block. The continue statement will end the current loop step of the loop control flow block in advance, even if the loop control flow block is not the innermost loop control flow block containing the continue statement.

The following is an example of using break and continue statements with labels.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import "fmt"

func FindSmallestPrimeLargerThan(n int) int {
Outer:
for n++; ; n++{
for i := 2; ; i++ {
switch {
case i * i > n:
break Outer
case n % i == 0:
continue Outer
}
}
}
return n
}

func main() {
for i := 90; i < 100; i++ {
n := FindSmallestPrimeLargerThan(i)
fmt.Print("The smallest prime number larger than ")
fmt.Println(i, "is", n)
}
}