Posts

Showing posts with the label go-programming

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...

Data Types in Go

Image
GOLang Data Types Data types are one of the key things one has to know about while learning a language. They constitute the base of any programming language. I will be covering major data types that any programmer would be using throughout the day while working with GO. Without wasting enough time lets start with GO data types. Basic Types int float32 float64 bool strings derived types Int Ints are used to store 32 or 64 bits integer values defined in GO as follows: var i int = 122 i:=122 The latter syntax is used when we want GO Compiler to pick the data type by inferring the value stored in the variable. Floats Floats are used to store decimal values in Go. Floats can be of two types float32 float64 These two types correspond to the amount of space 32 bytes and 64 bytes respectively to store the numeric values. floats can be created in Go using the following syntax: var f float32 = 1.1 var f64 float64 = 1.1 Bool Bo...