Posts

Showing posts with the label getting started with GO

Condition Construct in GOLang

Image
Condition Construct in GOLang Conditional programming is a very important construct available in all the programming languages. Go provides 2 conditional programming constructs If else Switch If Else This is a very basic construct and is similar to most of the other languages. If else can be written in the following way: package main import "fmt" func main() { i := 10 if i > 10 { fmt.Println("Greater than 10") } else { fmt.Println("Less than 10") } } We can also create an IF Else ladder in Golang package main import "fmt" func main() { i := 7 if i > 10 { fmt.Println("Greater than 10") } else if i > 5 { fmt.Println("Greater than 5") } else { fmt.Println("Less than 5") } } Switch Statement Another alternative to IF Else is a Switch statement which is quite useful in Golang. Following is a very basic example of a switch statement pa

Loops in GoLang

Image
Loops Loops are one of the iterative constructs used in all programming languages to perform some task iteratively or to recurse over a data structure like a map or an array. Many languages provide a set of FOR, WHILE and DO WHILE   loop but GoLang provides a different approach towards these three loops. The only looping constructs available in Go is FOR.   Let's see how we can achieve different looping scenarios using GO. Classic For loop Following is an example of how we can write the classic for loop with initialization, condition and increment step. package main import "fmt" func main() { var i int //for initialization;condition; increment/decrement for i = 0; i < 10; i++ { fmt.Println("Inside the loop") } } For with just condition (While loop) A while loop kind construct can be achieved using the for loop with just a condition and performing the initialization step before the loop and increment/decrement step insi

Advanced Data Types in Go

Image
Introduction In my previous blog , I discussed the basic data types in Go. In this blog, we will be going a step ahead and learning about advanced data types in GO. We will be talking about Arrays Slices Maps Structs Arrays Arrays as in other traditional languages are a collection of variables of homogeneous types i.e means instead of creating 10 variables to store same type of data let's say marks we create an array that can store 10 elements. Some of the key points about arrays in Go are: Arrays in Go are fixed size Array index starts from 0 Same type of data can be stored in an array Data Type has to be given while the array declaration The syntax for array declaration: //Array declaration var arr [3]int //Indexing the array arr[0]=1 arr[1]=2 arr[2]=3 We can also use shorthand notation for the Arrays //Array declaration and initialization arr1 := [3]int{1, 2, 3} Slices Slices, on the other hand, is a data structure more flexib

Writing first Go program

Image
Introduction Nowadays there is a new buzz word in the IT world i.e  Golang . The need for a highly efficient yet simple programming language has lead to the creation of this new language name Go. As per official  documentation , Go is The Go programming language is an open source project to make programmers more productive.   Go is expressive, concise, clean, and efficient. 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 yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language. Being an IT professional and in my struggle for constantly updating myself with new tools I started learning Go. I will be sharing my progress by writing blogs on my learnings.