Use statements in logic of code

If – else if – else

Branching with "if" "else if" and else in Go is straight-forward.

You can use this syntax to do difference logic code base on condition which you send to statements.

In this example, I use a build-in lib of golang to get time now of your PC in Unix format, that is a number of integer

time.Now().Unix()

So, you need to import it to use “time”

Now, we want to check n with

% 2 is 0 (that mean n Divisible by 2) and the same, % 4, % 6. If this condition is true. Print stuff look like n % 4 == 0 and n

else -> print only value of n

// basic syntax
if condition1 {
// TODO
} else if condition2 {
// TODO
} else if condition 3 {
// TODO
} else {
// TODO
}
package main

import (
	"fmt"
	"time"
)

// for if/esle statements
func main() {
	n := time.Now().Unix()
	// if - else if - else
	if n%2 == 0 {
		fmt.Println("n % 2 == 0", n)
	} else if n%4 == 0 {
		fmt.Println("n % 4 == 0", n)
	} else if n%6 == 0 {
		fmt.Println("n % 6 == 0", n)
	} else {
		fmt.Println("n == ", n)
	}
}

n % 2
n can not % 2, 4, 6

Loop logic

When you want to do something and that logic repeatedly, you can use “for” statements. Other name: for loop

In this tutorial you will learn how to repeat a block of code execution using loops in Golang.

Golang – traditional for Statement

The for loop is used when you know in advance how many times the script should run.

Consider the following example, display the numbers from 1 to 10 in three different ways.

package main
 
import "fmt"
 
func main() {
 
  k := 1
  for ; k <= 10; k++ {
    fmt.Println(k)
  }
 
  k = 1
  for k <= 10 {
    fmt.Println(k)
    k++
  }
 
  for k := 1; ; k++ {
    fmt.Println(k)
    if k == 10 {
      break
    }
  }
}

Golang – for range Statement (in next article, I will show you)

comming soon …

Switch and case

If you understood if/else statement, “switch/case” is an other branching statement. You can also use this syntax to do difference logic code base on condition which you send to statements. But, with switch/case, we can use condition base on value of variable or a complex comparison

Here’s a basic switch syntax.

variable := real_value
switch variable {
case value1: // real_value == value1
{// TODO}
case value2: // real_value == value2
{// TODO}
case ...: // real_value == ...
{// TODO}
default:
{// no case matched -> do something as default}
}

If you not use variable for switch, case is a condition -> the same with if/else statement

switch {
    case condition1: 
    {
    }
    case condition2:
    {
    }
    ...
    default: 
    {
    }
}

Example with basic

i := 2
    fmt.Print("Write ", i, " as ")
    switch i {
    case 1:
        fmt.Println("one")
    case 2:
        fmt.Println("two")
    case 3:
        fmt.Println("three")
    }

You can use commas to separate multiple expressions in the same case statement. We use the optional defaultcase in this example as well.

i := 2
switch {
    case 1, 2:
        fmt.Println(i)
    default:
        fmt.Println("100000")
    }

switch without an expression is an alternate way to express if/else logic

t := time.Now()
    switch {
    case t.Hour() < 12:
        fmt.Println("It's before noon")
    default:
        fmt.Println("It's after noon")
    }

View on github

PreNext