
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Demonstrate Begin and End Blocks
In this Go language article, we will write programs to demonstrate BEGIN and END blocks using sum of numbers, iteration and two variables.
Blocks are used to group the statements where the variables described in the block can only be used in that scope and not outside it. They also help in code readability
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 and in this function create two begin and end blocks enclosed within curly braces.
Step 3 ? In the first block take two numbers in two variables named a and b, add these numbers and assign them to c.
Step 4 ? Print the c on the console using Printf function from fmt package.
Step 5 ? In the second block take the name and age of a person and print it similarly like we did in last step.
Example 1
In this Example, we will create two begin and end blocks in the main function. In the first block we will print the sum of two numbers and in the second block we will print the name and age of a person.
package main import "fmt" func main() { { a := 10 b := 20 c := a + b fmt.Printf("The sum of %d and %d is %d\n", a, b, c) } { name := "Ronit" age := 34 fmt.Printf("%s is %d years old\n", name, age) } }
Output
The sum of 10 and 20 is 30 Ronit is 34 years old
Example 2
In this illustration, we will create two begin and end block and an anonymous function will be used to execute the begin and end block.In the first block some statements will be executed whereas in the second block numbers will be printed using iteration.
package main import "fmt" func main() { func() { defer fmt.Println("This statement is executed last in the program") fmt.Println("This statement is executed first in the program") }() func() { for i := 1; i<= 6; i++ { defer fmt.Printf("%d ", i) } }() }
Output
This statement is executed first in the program This statement is executed last in the program 6 5 4 3 2 1
Example 3
In this Example, we will write a Go language program to demonstrate the BEGIN and END blocks using two variables a and b.
package main import "fmt" func main() { { a := 20 fmt.Println("Value of x inside begin block:", a) } { b := "Hello, alexa!" fmt.Println("Value of y inside begin block:", b) } }
Output
Value of x inside begin block: 20 Value of y inside begin block: Hello, alexa!
Conclusion
We executed the program of demonstrating the begin and end blocks. In the first Example, we created two blocks and printed the output using the fmt package whereas in the second Example, we used defer keyword while printing the output of blocks and in the third Example, we used two variables to execute the program.