Golang basics series #3
Conditional If and conditional switch in golang
So far we tried to see some basic concept of not only for golang but also a concept's that are applicable across multiple programing languages
some of the concept we covered in recent articles
- package
- variable
- constant
- function
- data types
if you want to refresh your self you can get back to the article vai the link below
https://blog.lolinemag.com/article/ea2b5b0b-cba0-44d9-b782-679fdabc96b2/
so let's continue our journey in basics of golang and explore more concepts
Conditionals If
Conditionals in Go are similar to conditionals in other languages. The underlying type of any conditional operation is the bool type, which can have the value of true or false. Conditionals are often used as flow control mechanisms to check for various conditions. For checking a particular case an if statement can be used, which executes its code if the underlying condition is true like this:
In scenarios involving more than one case many if statements can be chained together using the else if and else statements.
However, it is convention to avoid else statements as Go promotes early returns:
If statements can also include a short initialization statement that can be used to initialize one or more variables for the if statement. For example:
Note: any variables created in the initialization statement go out of scope after the end of the if statement.
Coming from other languages one may be tempted to try to use one-line conditionals. Go does not support ternary operators or one-line conditionals. This is a purposeful design decision to limit the amount of possibilities, making code simpler and easier to read.
Conditionals Switch
Like other languages, Go also provides a switch statement. Switch statements are a shorter way to write long if ... else if statements.
Basic usage
To make a switch, we start by using the keyword switch followed by a value or expression. We then declare each one of the conditions with the case keyword. We can also declare a default case, that will run when none of the previous case conditions matched:
If we want to run the same piece of code for several cases, we can group them together in a single case, separating them with a ,:
Cases with boolean expressions
One interesting thing about switch statements, is that the value after the switch keyword can be omitted, and we can have boolean conditions for each case. This effectively can be a shorter way to write complex if ... else statements:
Fallthrough
When the condition in a case matches, the corresponding code will run and Go will not evaluate the other case conditions by default. We can make use of the fallthrough keyword to tell Go to evaluate the other case conditions. Take this example:
We can correct this code by using the fallthrough keyword:
in the upcoming series we will hold introducing new concept and we will solve some problem by the concept we learnt so far
previous article
https://blog.lolinemag.com/article/ea2b5b0b-cba0-44d9-b782-679fdabc96b2/