Golang basics series #3

6 min read
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:

var value string if value == "val" { return "was val" }

In scenarios involving more than one case many if statements can be chained together using the else if and else statements.

var number int result := "This number is " if number > 0 { result += "positive" } else if number < 0 { result += "negative" } else { result += "zero" }

However, it is convention to avoid else statements as Go promotes early returns:

func getVal(connected bool) int { // The exceptional case should be in the `if` statemeent. // In this case being `connected` is the default, `readLocalVal` the fallback. if !connected { // using an `early return` to remove the need for an `else` case return readLocalVal() } return readVal() }

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:

num := 7 if v := 2 * num; v > 10 { fmt.Println(v) } else { fmt.Println(num) } // Output: 14
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:

operatingSystem := "windows" switch operatingSystem { case "windows": // do something if the operating system is windows case "linux": // do something if the operating system is linux case "macos": // do something if the operating system is macos default: // do something if the operating system is none of the above }

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 ,:

operatingSystem := "windows" switch operatingSystem { case "windows", "linux": // do something if the operating system is windows or linux case "macos": // do something if the operating system is macos default: // do something if the operating system is none of the above }

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:

age := 21 switch { case age > 20 && age < 30: // do something if age is between 20 and 30 case age == 10: // do something if age is equal to 10 default: // do something else for every other case }

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:

age := 21 switch { case age > 20: // do something if age is greater than 20 case age > 30: // WARNING: code here will never run. If age is greater than 30, // it is also greater than 20, which means only the previous case will run default: // do something else for every other case }

We can correct this code by using the fallthrough keyword:

age := 21 switch { case age > 20: // do something if age is greater than 20 fallthrough case age > 30: // Since the previous case uses 'fallthrough', // this code will now run if age is also greater than 30 default: // do something else for every other case }

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/

Comments (0)
No comments yet