Part 3 - Golang Basics (Switch Case, Type inference & Defer Keyword)

Part 3 - Golang Basics (Switch Case, Type inference & Defer Keyword)

ยท

3 min read

Hey again, Today in this Golang series I will explain a few easy but important topics, these topics are very common to come up in a real Golang interview.

TABLE OF CONTENT

  1. Switch Case,

  2. Type Inference,

  3. Defer Keyword

SWITCH CASE

In part 2 of the Golang series we have already seen the if-else block to conditionally execute a block of code, the switch case is just a fancier and easier way to handle when we have multiple conditions. Here is an example to better understand Switch Case.

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    num := rand.Intn(10) // this will genarate random integer between 0 and 10,

    switch num % 2 {
    case 0:
        fmt.Println("EVEN NUMBER", num)
    case 1:
        fmt.Println("ODD NUMBER", num)
    default:
        fmt.Println("INVALID NUMBER", num)
    }
}

If you are coming from any other programming language like C++ or Java you must be thinking we need a break statement in every case statement but in Golang we don't need that as it automatically adds a breaks statement after every case statement, If we don't want that feature though we can always use fallthrough.

Here is an example,

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    num := rand.Intn(10) // this will genarate random integer between 0 and 10,

    switch num % 2 {
    case 0:
        fmt.Println("EVEN NUMBER", num)
        fallthrough
    case 1:
        fmt.Println("ODD NUMBER", num)
        fallthrough
    default:
        fmt.Println("INVALID NUMBER", num)
    }
}
/*
    suppose num is 6
    then it will print
    ------------------
    ODD NUMBER 6
    EVEN NUMBER 6
    INVALID NUMBER 6
    ------------------ 
*/

TYPE INFERENCE

Type inference is one of the fundamental topics of any static-typed programming language which is just changing one type to another.

It would be easier if I gave an example to you.

package main

import "fmt"

func main() {
    var num float32 = 7.23
    var num2 int = int(num)
    fmt.Println(num, num2) // 7.23 7
}

In the above example, I am converting from float32 to int type for that reason we lost some value after the decimal point it is called lossy type conversion. We can convert from int to string also by using the fmt package only.

Let's take an example,

package main

import "fmt"

func main() {
    var x int = 7
    var y string = fmt.Sprintf("%x", x)
    fmt.Println(y)
}

DEFER STATEMENT

Defer statement is one of the most asked, important topics in a golang interview and it is one of the easiest topics of all in golang.

"A defer statement executes after all other non-deferred statements and follows the Last-In-First-Out (LIFO) order." - You just have to remember that.

Let's take an example.

package main

import "fmt"

func main() {
    defer fmt.Println("1")
    defer fmt.Println("2")
    defer fmt.Println("3")
}
/*
    CODE OUTPUT
    ------------
    3
    2
    1
*/

A use case of a defer statement is when we do a db connection as the db connection should be executed after all got initialized.

We can even defer a single function. Here is an example.

package main

import "fmt"

func myFunction() {
    fmt.Println("1")
    fmt.Println("2")
    fmt.Println("3")
}
func main() {
    defer myFunction()
    fmt.Println("4")
}

/*
    CODE OUTPUT
    ----------
    4
    1
    2
    3
*/

That's it guys and congo ๐Ÿฅณ you have learned the most important topics in this blog. In the next part, you will learn all about functions in Golang. Stay tuned ๐Ÿ˜ƒ.

ย