
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
Create a Block Using Curly Braces in Golang
We can create a block in golang using curly braces and then create a variable inside the block to have the scope only inside the block and not outside it. In this article, we will use three examples to create a block using curly braces. In the first example few statements will be printed inside and outside the block, in the second example comparison value will be printed inside the block and in the third example function will be used to demonstrate the use of block.
Algorithm
Import the required packages in the program
Create a main function
In the main create a block using curly braces and execute some statements inside and outside the block
The print statement is executed using Println function from the fmt package
Example 1
In this example, we will create a main function and, in that function, we will create a block using curly braces. Two statements will be executed inside the block and one statement will be executed outside the block. Let's see how the practical implementation is done.
//Golang program to create a block using curly braces package main import "fmt" //Main function to execute the program func main() { // Creating a block using curly braces { fmt.Println("This is the first statement inside the block!") fmt.Println("This block executes two statements") } fmt.Println("This is the statement outside of the block!") }
Output
This is the first statement inside the block! This block executes two statements This is the statement outside of the block!
Example 2
In this illustration, a main function will be created in which a variable a with a value is compared with a constant inside the block created using curly braces. Then, two more statements are executed inside the block to show the program's execution.
//Golang program to create a block using curly braces package main import "fmt" //Main function to execute the program func main() { a := 10 if a > 6 { fmt.Println("a is greater than 6") { fmt.Println("This statement is inside the block!") fmt.Println("Anything inside curly braces is part of the block.") } } }
Output
a is greater than 6 This statement is inside the block! Anything inside curly braces is part of the block.
Example 3
In this example, we will create a function in which statements will be executed inside the function. The function will be assigned to a variable which will be called to execute the program. Further, one statement is executed outside the block.
//Golang program to create a block using curly braces package main import "fmt" //Main function to execute the program func main() { // Define a function with a block using curly braces myFunction := func() { fmt.Println("This is inside the block of a function!") fmt.Println("Anything inside curly braces is part of the block.") } // Call the function myFunction() fmt.Println("This is outside the block!") }
Output
This is inside the block of a function! Anything inside curly braces is part of the block. This is outside the block!
Conclusion
We executed and compiled the program of creating a block using curly braces in three examples. In the first example, we printed few statements outside the block and few inside it. In the second example, we printed the comparison of the value in a variable with a constant inside the block and in the third example, we used a function to execute the operation.