You Should Know these things Conditional Or Control Flow Statement 2019
Check This Previous blog post written by Sagar Jaybhay for better understanding.
- Part 1: https://sagarjaybhay.net/google-go-tutorial-by-sagar-jaybhay-blog-1/
- Part 2: https://sagarjaybhay.net/packages-and-golang-command-part-2/
- Part 3: https://sagarjaybhay.net/detail-datatypes-in-golang-you-should-know-2019/
- Part 4:https://sagarjaybhay.net/tutorial-go-language-constant-in-golang-2019/
You Should Know This Things About conditional statement or Control Flow Statement in Golang
Conditional Statement In Golang
If statement
Like any other programming language golang also support for conditional statement and it allows a developer to execute code based on condition result in the term in if statement. The if statement executes based on true or false.
In if expression need not be surrounded by parentheses ( )
but the braces { }
are required.
If statement: it executes only based on a condition is true which is only one condition.
if i > 9 || i < 12 { fmt.Println("Hello, Mr.Sagar Jaybhay")
If..else: it is having 2 conditions and if one is true it will execute if block if not then it will execute else block.
package main import ( "fmt" ) func main() { var i = 1 if i > 9 || i < 12 { fmt.Println("Hello, Mr.Sagar Jaybhay") } else { fmt.Println("Bye Sagar Jaybhay") } }
If..else if…else: in these 2 conditions are managed if the first condition is not true then it will go to else if the block it will check that if it is not true then it will execute else block any condition is true then it will execute that respective block. The feature is similar to the regular programming language code for this is below.
If you want you can write multiple else if block in this statement.
package main import ( "fmt" ) func main() { var i = 1 if i > 9 { fmt.Println("i m in if block") } else if i < 1 { fmt.Println("i am in else if block") } else { fmt.Println("i am in else block") } }

Nested If else
You can also write a nested if-else statement as per your requirement. See below code
package main import ( "fmt" ) func main() { var i = 1 if 1 == 1 { if i > 9 { fmt.Println("i m in if block") } else if i < 1 { fmt.Println("i am in else if block") } else { fmt.Println("i am in else block") } } else { fmt.Println("variable i not equal to 1") } }

Control Flow Statement in Golang
Switch Statement in golang
A switch statement in golang is the same as the switch statement in other languages like Java, C#, C, and C++.
The switch statement is a shorter way to write an if-else statement. The subtle difference between other language switch statements and golang switch statements is that golang automatically inserts break statements at the end of the case statement but in other languages, you need to specify break statements explicitly. A second important difference is that golang cases need not be constants and values of a case be anything.
package main import "fmt" func main() { var a = 2 switch a { case 1: fmt.Printf("1") case 2: fmt.Printf("2") case 3: fmt.Printf("3") } }
See this above code in this we not added break statement even though the output will print only 2 value as shown in the below image.

Switch statement flow diagram

Switch evaluates its cases from top to bottom and it stops when the case succeeds. Another thing is that in most programming languages switch statements you need to pass value but in golang, you no need to pass any value it evaluates as true and executes the conditions. See below example we are not passing any value to switch statement and in case statement we evaluate the expression based on that condition is executed.
package main import "fmt" func main() { var a = 2 switch a { case 1: fmt.Printf("1") case 2: fmt.Printf("2") case 3: fmt.Printf("3") } anotherswitch() } func anotherswitch() { println("i am in another_switch function") var number = 40 switch { case number == 10: println("number is 10") number = number + 10 case number == 20: println("number is 20") default: println("nothing has found") } println("number is ", number) }