Monday, 2 October 2017

4 - Go Control Statements

In last small sessions…

  • we
    • Learnt how to create simple variables
    • The syntax to create structures
    • Create objects of structure types and use their properties
  • Previously, we studied about what golang is offering new and different IDEs for golang

Today…
  • Control statements are the lines of code which creates various branches of code execution according to the mentioned condition
  • Go-Language Control Statements:
    • If-else
    • Switch-case
    • For
    • Reuse of variables
    • Conditions
  • But before that, if you haven’t subscribed to my blog and channel already then subscribe to get all the latest updates of activities in my blog and youtube channel
  • We will take a look at few syntaxes of control statements.

    GO If

if condition {
========Body=======
} else if condition {
========Body=======
} else {
========Body=======
}
Else if or else part is not compulsory
•Example:
if gender == “male” {
  name = “Mr ” + name
} else if gender == “female” && ismarried == true {
  name = “Mrs “ + name
} else {
  name = “Ms ” + name
}
fmt.println(“Hello ” + name)
Here, we can have as many else if as we require. Every if statement creates a branch in code execution flow. From that statement, there will be possibilities of different routes.

GO Switch-Case
Consider a scenario,
if base == 2 {
  fmt.println(“binary”)
} else if base == 16 {
  fmt.println(“hexa-decimal”)
} else if base == 8 {
  fmt.println(“octal”)
} else if base == 10 {
  fmt.println(“decimal”)
} else {
  fmt.println(“custom”)
}
Here, same variable value is referred five times and conditions are placed. Now, in such cases, we might want to reduce our work by have something simple.
switch {
  case base == 2 :
    fmt.println(“binary”)
  case base == 16 :
    fmt.println(“hexa-decimal”)
  case base == 8 :
    fmt.println(“octal”)
  case base == 10 :
    fmt.println(“decimal”)
  default :
    fmt.println(“custom”)
}
switch base {
  case 2 :
  fmt.println(“binary”)
  case 16 :
  fmt.println(“hexa-decimal”)
  case 8 :
  fmt.println(“octal”)
  case 10 :
  fmt.println(“decimal”)
  default :
  fmt.println(“custom”)
}

GO For Loop
Go-language for loop is similar to those available in other language. However, in golang, for loop provides more facilities than other languages by providing 3 types of syntax.
  1. Normal for-loop
  2. While loop functionality in for-loop
  3. For loop to handle multiple return values

According to the scenario and requirements, usages will differ. We’ll go through each of them.
Normal For loop
Øfor initialization; control-condition; iteration { }
While loop functionality in for loop
Øfor control-condition { }
For loop with multiple return values
Øfor var1, var2 := range collection(slice, map, etc.) { }
Normal For loop
for index := 0; index < 10; index++ {
  sum = sum + index
}
As we know that, Operator := performs the creation as well as assignment, we can directly use it in for loop.
We can also use already declared variable instead of index (new variable). Just, in that case, := will not be used. Just = will be used.
This loop is as other languages.
While loop functionality in for loop
for control-condition { }
Here, index should be declared…
for index > 0 {
  sum = sum + index;
  index--;
}
For loop with multiple return values
for var1, var2 := range collection(slice, map, etc.) { }
For example:
In case of map----
for key, val := range mapvar {
  fmt.Println(“Key = ” + key + “, Value = ”  val);
}
In case of slice (with same syntax)---
for index, element := range slicevar {
  fmt.Println(“At index ” + index + “, element is ” + element);
}
In case, we just want to iterate over index or key and we don’t want to spare the memory for value part during the for loop, then we can just omit that variable.
for key := range mapvar {
  fmt.Println(“Key = ” + key);
}
Let’s say, we just want to iterate over value or element part. In that case, our for loop will look like,
for _, val := range mapvar {
  fmt.Println(“Value = ”  val);
}

GO Reuse of variables
- While developing go-framework, they kept the memory concerns in considerations.
- Golang is also popular for its optimized memory management.
- When we say that := creates a new variable, then it also manages the old variable.
- For example,
out, err := package.FunctionCall(args)
Creates 2 variables out and err. Now, in the same context if we use
val, err := anotherpackage.FunctionCall(arguments)
Creates 1 variable val and err will be re-used. It’ll just override the previous results

GO Conditions
- In every language, control statements use conditions to decide the flow.
- There are various operators which can be used to create conditions
Operators like
- >, <, >=, <=, == : Value comparison with 2 operands
- && logical and : both conditions should be true to get the final result true
- || logical or : both conditions should  be false to get the final result false
- There are many other operators which performs different tasks.
- For examples:
var1 > var2  var1 < var2  var1 >= var2  var1 <= var2  var1 == var2
var1 == var2 && var3 == var2
var1 <= var2 || var2 >= var3
string1 == string2
……. Etc.


4 - Go Control Statements

In last small sessions… we Learnt how to create simple variables The syntax to create structures Create objects of structure types a...