Skip to content

Go Programming

Code Example – If Else in Go Programming

Google’s Go programming language (Golang) is gaining momentum and IMHO it has the potential to become one of the most popular languages in near future. It’s clean, simple and at the same time addresses a few of the challenges of modern computing. There’s no statistics to support but Go is getting popular with programmers of almost all the languages. From whatever unstructured data is available on the web it seems Go is getting highly popular among Ruby & Python programmers.

This is the 3rd post on Golang at CodingThis. You can read the last one here Google Go Programming – For Loop Adventure.

The purpose of this article is to generate curiosity among Go newcomers. That’s the reason I’m sharing a construct that is available in every language – if else.

Let us see the code below:

package main

import (

       "fmt"

       "time"

       "strconv" )

func main() {

        str := "Enter your name and amount to be withdrawn?"  

        var name string

        var withdraw int

        today := time.Now().Format(time.RFC850)

        fmt.Println(str)

        fmt.Scanf("%s %d", &name, &withdraw)

        fmt.Println("Hello", name+": your bank balance on " +today+ " is

                    only $100")

        if withdraw >= 100 {

        fmt.Println("You can't withdraw $" +strconv.Itoa(withdraw)+" - Insufficient                          balance")

         } else { fmt.Println("Transaction in process...")

       }

}

Once you have installed Go in your system, open a notepad and copy paste the above code. Execute the code. You will get the output as follows:

1. If you enter the withdrawal amount as 100 or  more than 100 then you get the following output of insufficient balance:

Goifelse (2)

2. If you enter the withdrawal amount as  less than 100 then you get the following output of transaction in process:

Goifelse (3)

Learning from the above code snippet:

  1. Variable declaration
  2. Use of Scanf function
  3. Use of if else

Please share and spread the word if you think it can help a Go new comer.

Be Sociable, Share!
    The following two tabs change content below.

    Basant Singh

    Basant is a talented software developer who is very engaged in web & cloud platforms on a daily basis. He is a contributor to codingthis.com reporting on various news and troubleshooting on various platforms. Basant is very strong in SQL and a great asset to the CodingThis community.

    Latest posts by Basant Singh (see all)