DEV Community

Cover image for Day 12 of #30DaysOfCode | Conditionals In Go (golang)
Shubham Saurav
Shubham Saurav

Posted on

Day 12 of #30DaysOfCode | Conditionals In Go (golang)

Hey wonderful people! How are you doing today? I hope you are healthy in the pandemic. I hope that you are living your life and learning new things every day. Today is Day 12 of #30DaysOfCode challenge and I learnt about conditionals in Go.

Whenever you are writing a program, you don't want to execute from the first line to the last line. You want to have control over your program. You want to run some lines code only when a certain situation is met and the conditionals help you to do so.

The conditionals help you to control the flow of your program. In Golang, we have many conditionals. These conditionals are very similar to other C like languages. Let's talk about them one by one.

  1. If conditional: The if conditional executed the code inside its block when a condition is true.

  2. If-Else conditional: The if-else conditional executes the code inside the if block when a certain condition is true and it executed code in else block when the condition is false.

  3. If, Else-If and Else conditional: The if, else if, else conditional execute the code in if block when the if condition is true. If the condition is not true, it moves to the else if block and executes the code in its block if the condition in else if is true. If none of the if and else if conditions are true, it execute the code in the else block.

  4. Switch Case statement: The switch case statement is used to select one of many blocks of code to be executed. It executes the code of a case when the condition for that case is true.

These conditionals work almost the same in practice in Go as they work in other C-like languages. Here is a simple example of if, else if, else in Go.

Conditional exanple in golang

Alright! That's gonna be it for today's post.
Bye.

Connect With Me:
Youtube: ShubhamSauravYT.
Twitter: @hiShubhamSaurav
Instagram: @hiShubhamSaurav
Facebook: @hiShubhamSaurav

Top comments (0)