From this blog you will come to know about What is GO programming, why to learn, what makes it special, how it is different from other programming languages and writing the basic code samples in go.
Background (My Experience)
I started learning programming languages as part of computer science programming, initially not able to understand anything. I started with C and C++, even after 6 months into learning it made no sense for me. After getting started to know about Python everything became easy.
Python is easy to read, write and understand, building web applications, machine learning projects, automation scripts, rest Api's and data cleaning pipelines. Python is becoming a go to language for beginners and top 5 preferred languages in the industry.
I moved to Golang to understand memory management, error handling better, building scalable products and to overcome using python for everything.
Most of the basic tasks of a programming language are better handled in python by default like variables, datatypes, pointers, memory allocations, building custom packages, downloading libraries and so on. So, using python for a longer period may hamper while working on different tools.
In terms of memory management, concurrency, scalability, and distributed computing Golang is the go-to programming language. Golang can be used when an enormous number of people are using the software or app daily.
GO Introduction
Go is a statistically typed language, developed by Google, released in 2009. It is created to address huge problems faced by companies like google. It is also an opensource programming language.
We all know that more than millions of people use Google services every second, so the applications should be highly available, scalable, reliable, and better memory management.
GO is also called Golang, and these words are used interchangeably over the internet.
If you are already building applications using other programming languages, learning Golang helps you to write the better code, as it doesn’t allow to run the program if declared variables and packages are not used, where in other languages we can load the unused packages.
Even when we are using the functions, we must mention the return type of the value, which we want to retrieve from the function. During the declaration of different data structures, we also need to mention the datatypes.
Basic Program
Writing the Hello World programmed.
Variables
Declaring the variables in the go is straight forward by mentioning a datatype with a var before the variable.
Datatype is a value which we are providing to the computer, it helps the compiler with how to process the variable.
Examples :- string, integer, Boolean, floats … etc.
// here var is for declaring the variable
// a is variable and at the end is datatype
// var <variable> <datatype>
var a int32
a = 32
fmt.Printf("%d", a)
We can also declare the values directly.
a := 32
fmt.Printf("%d", a)
Similarly, we can declare the strings, Boolean values.
package main
import "fmt"
func main() {
//Print statement
fmt.Println("Defining the variables")
// writing variables in different formats
var name string = "Sainadh"
fmt.Println(name) //fmt has many other operations and functions
var friend string
friend = "Golang"
fmt.Println(friend)
// this is special case and we no need to give datatype
blog := "medium"
fmt.Println(blog)
var age int = 22
fmt.Println(age)
year := 2022
fmt.Println(year)
//printing the value and datatype
fmt.Printf("%v, %T", age, age)
}
Conditional statements
When it comes to conditional statements, we can use if conditions like any other programming language, but we don’t have when condition in place of it we can use for.
age := 18
// if condition { } else { }
if (age >= 18) {fmt.Println("Eligible to vote")} else {fmt.Println("Not Eligible to vote")}}
If conditions are very straight forward and they check for the conditions, if the condition is satisfied then only the program related to it is executed.
In the above program, it is checking whether a person is eligible to vote or not based on his age.
Sometimes we want to run a series of steps, until a condition is satisfied, in other programming languages e.g:- python, c, c++, we have while condition, here we have for condition.
i := 1
// for condition { code .. }
for i <= 5 {
fmt.Println(i)
i = i+1
}
Above code prints 1, 2, 3, 4, 5.
Here we are starting with one, condition is when i is less than 5, execute the below block.
Block is a set of code, which is executed based on a condition.
Loops
Loop is doing the same thing over a range of values. It is required in any programming language.
// for initialize; condition; post condition
for i := 1; i <= 5; i++ {
fmt.Println(i)
}
Here the code prints 1,2,3,4,5 values.
The loop runs until the condition is satisfied.
for is used as conditional statement and loop statement. It is help full when we are checking over a list and array of values, we will learn more about this in next blogs.
Taking INPUT
In any programming language, taking input from our console is essential for testing the code and writing it better.
Whatever input we give in Golang from command line, it is read as a string while using fmt.
Here is the basic program to take the input.
package main
import (
"fmt"
) func main(){ var name string
//reading from the cli
fmt.Scanln(&name)
fmt.Println("Your name is : ",name)
}
The above program is only useful to take input for a single word. When using os package to take input, the input is read as a buffer then the buffer is converted into string.
package main
import (
"fmt"
"bufio" //for multiple elements as input
"os"
)
func main() {
// reading from the cli for multiple words
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter a phrase: ") //here we are using '_' to ignore the error
name, _ := reader.ReadString('\n') fmt.Println("Your phrase is : ",name)
}
bufio is an input output buffer, which converts the input into string format.
When we enter that input, os will read it as buffer, using bufio we can convert it into string.
For any numerical values, we can extract them from the string using string converter.
Let’s use this as an example.
package main
import (
"fmt"
"bufio" //for multiple elements as input
"os"
"strconv" //for converting string to integers
"strings" // for trimming the extra spaces (in this case)
)func main(){
reader := bufio.NewReader(os.Stdin)
fmt.Println("Enter your age : ")
age, _ := reader.ReadString('\n')
ageNum, _ := strconv.ParseFloat(strings.TrimSpace(age), 64)
fmt.Println("Your age is increased by : ",ageNum+2)
}
We have learned about basics of go, such as declaring and defining the variables, taking input from the console, using the packages like fmt, os, bufio, strconv and strings.
These blogs are more about going with practical experience and getting your hands dirty.
If you are more interested in Golang follow this series of GO blogs and apply the knowledge gained on solving 30 Days of Coding on Hacker Rank using Golang.
If you have any questions, please comment below.
Reference: -