Golang program to create multiple BEGIN and END blocks


In this article, we will learn to write Go language programs that will create multiple BEGIN and END blocks using curly braces, “conditional statements”, as well as “external functions”.

A block is created using curly braces, the scope of a variable remains inside the block and not outside it.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a main function

  • Step 3 − In the main, create a first block by initializing a variable

  • Step 4 − Print the a variable on the console for the first block

  • Step 5 − Create a second block by initializing a variable

  • Step 6 − Print the value of a variable on the console for the second block

  • Step 7 − Then, create a third block and initialize a variable in the block

  • Step 8 − Print the value of the a inside the third block

  • Step 9 − The print statement is executed the Println function from the fmt package where ln means new line

Example 1

In this Example, we will create a main function and, in that function, we will create multiple blocks using curly braces. In each block print the desired output on the console.

package main

import "fmt"

func main() {
	
   {
      a := 1
      fmt.Println("Value of a inside first block is:", a)
   }
	
   {
      a := 2
      fmt.Println("Value of a inside second block is:", a)
   }
	
   {
      a := 3
      fmt.Println("Value of a inside third block is:", a)
   }
}

Output

Value of a inside first block is: 1
Value of a inside second block is: 2
Value of a inside third block is: 3

Example 2

In this Example, a main function will be created and, in that function, create three blocks with if conditional statement in each block without any condition by setting the value true.

package main

import "fmt"

func main() {
	
   if true {
      a := 1
      fmt.Println("Value of a inside first block is:", a)
   }
	
   if true {
      a := 2
      fmt.Println("Value of a inside second block is:", a)
   }
	
   if true {
      a := 3
      fmt.Println("Value of a inside third block is:", a)
   }
}

Output

Value of a inside first block is: 1
Value of a inside second block is: 2
Value of a inside third block is: 3

Example 3

In this Example, we will write a Go language program to create multiple BEGIN and END blocks using three external functions.

package main

import "fmt"

func main() {
	
   firstBlock()
   
   secondBlock()
	
   thirdBlock()
}

func firstBlock() {
   a := 1
   fmt.Println("Value of a inside first block is:", a)
}

func secondBlock() {
   a := 2
   fmt.Println("Value of a inside second block:", a)
}

func thirdBlock() {
   a := 3
   fmt.Println("Value of a inside third block:", a)
}

Output

Value of a inside first block is: 1
Value of a inside second block: 2
Value of a inside third block: 3

Conclusion

We executed the program of creating multiple BEGIN and END blocks. In the first Example, we used multiple curly braces to create multiple blocks whereas in the second Example we used if conditional statement with no condition and in the third Example we used external functions to execute the program.

Updated on: 03-May-2023

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements