Skip to main content

Go Conditional Statements and Loops


Conditional statement is those statement, which is used to execute code on a certain condition. Unlike C or Java conditional statement, conditions are not required to be written inside parenthesis in Go.



Lets dive into different conditional statement. Go has following conditional statements are:

1. If condition
2. If-else condition
3. If-else if-else condition

1. If condition:

If condition used to check true statement only, means if condition is true then the enclosed code will be executed other wise do nothing.

Suppose we've a variable with value as "Mango" and we want some task to perform while condition is true, so above line of code will execute with true condition and will print the message. If the condition isn't true then the code will do nothing.

2. if-else condition:

        Another form of conditional statement is "If-else" condition, it has two part, the first part will execute while the condition will be true otherwise the second part will be execute automatically.
Let's take an example.


Here we first check a condition for fruit, is fruit equal to Mango? And the fruit variable contain Orange, so the condition will be failed and else part code will be execute automatically and print on console.
"Fruit is different from Mango"

3. If-else if-else condition:

       This type of conditional statement checks multiple condition. we can also say that the multiple "if-else".

As we can see that we're using multiple "if else" condition. The code will be execute where it matches the condition, and at last if there isn't match with anyone condition that time else part will be execute automatically.

Short Statement with if condition:

Another variants for if statement, we can check condition with pre-assigned value to variable with single if statement. Lets take an above example as short statement in if condition.

In above example we first assign value to fruit then we're checking condition in single if statement. When we assign value to variable we put semicolon(;) so that Go compiler can understand that this code is use to assign value then checks condition statement will be execute.

Note: Go does not support ternary operator like C.

We have only the one way, "if condition" to achieve this(ternary operator). see below screen.



Switch Statement:

Switch statement is an alternate for multiple if-else condition, it uses case instead of multiple else if check condition. It executes matched case content with conditional expression.
Unlike C, C++ and Java, there is no need to use break statement in each cases. Go compiler adds automatically.


Loops

Loops are a block of content which execute again and again until the condition statement has been failed. Unlike C, C++ and Java there is no while and do while loop. Go has only one loop named as for loop. Also Go loop does not uses parenthesis before and after statement.



Above screen shows simple For loop which executes "Println()" six times. In Above example, it initialize 0 (zero) to variable i then it checks, is i value less than 5 (condition true until i hold value 6). If condition return true then underlined code will be execute then after it increases i value.

Note:- All statements of  For loop are optional.

There are different ways through which we can use For loop in our code. see below picture. All syntax are valid code.


For Range

In Go there is another form of loop, which is used to iterate over array or slice with key value pair.


Here in the example, we took an slice of string type then we used for with range keyword. There is two value will be return, first one as key of the slice and second one as the value on specific key in array/slice.
When we'll execute this code snippet will get following out put.



Comments

Popular posts from this blog

Apache Tika server using Golang

Apache Tika?      Apache Tika is a toolkit/library that uses to detect meta data and extract contents from different types of files (such as .txt, .docx, .pdf, .ppt etc).      Tika parsed all types of file through single interface, with Tika make useful for search engine indexing, content analysis and much more. Configuration To setup a tika server on local machine, you need to go download   page. Which looks like below image. From download page just download with the 3rd link Viz. "Mirrors for tika-server-1.25.jar". After download completion, go to the downloaded file and open a command prompt. Inside command prompt type below command to start tika server. java -jar tika-server-1.25.jar Now your tika server has been started on local environment and it'll provide an URL to access tika server. http://localhost:9998/ After configure tika server go to your GOPATH directory, create a file with name as you want to give file name with .go extension. I'm taking as "

What is Go/golang?

“Go will be the server language of the future.” — Tobias Lütke, Shopify Go (often referred to as Golang) is an open source programming language that makes it easy to build simple, reliable, and efficient software. Golang is a statically type language and compiled programing language. Go is similar to C, but with memory safety, garbage collection. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code. The world was first introduced to Go in November 2009 by Google’s Rob Pike, Robert Griesemer, and Ken Thompson. The main goal of creating Go was to combine the best features of other programming languages. Go released its latest version 1.15.6 on 3rd December 2020. A very basic Hello world program with Go: package main import "fmt" func main () { fmt . Println ( "Hell