Posts

Showing posts with the label GO

Enums in Go - Iota

Image
 Yes, you read it right. Like many other languages Go support the concept of enums through the IOTA function. IOTA According to Go documentation  Within a  constant declaration , the predeclared identifier  iota  represents successive untyped integer  constants . Its value is the index of the respective  ConstSpec  in that constant declaration, starting at zero. It can be used to construct a set of related constants.   Don't worry if you didn't get it. Basically, Iota is a keyword in Go that helps you to give random values to your constant. Let's say you want to define 7 constant Monday, Tuesday, Wednesday .... Sunday. Easy way to do this is const ( Monday = 1 Tuesday = 2 Wednesday = 3 Thursday = 4 Friday = 5 Saturday = 6 ) In this situation, values 1,2,3,4,5,6 don't make much sense. The only thing these values should follow is that they have to be unique i.e Monday should ...

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

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

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