In this post, we will go through Golang programming language basics.

This post assumes you already have programming experience with some other programming language like C, Python, Java etc.

Comments

There are two type of comments in golang:

Single Line

// This is a single comment

Multi Line

/*
This is 
a multi 
line comment
*/

Document

// A document comment is a normal comment written just before function, class or package declaration
func add(x int, y int) int {
    return x + y
}

Variables

There are three ways of declaring and using variables.

Method 1: Declare by specifying data type

var x int = 5

Method 2: Declare and let golang decide data type

var x = 6

Method 3: Use variable directly when required and let golang decide data type

x := 7

Constants

Constants have fixed values and their value cannot be changed. We can declare constants like this:

const PI = 3.142

Conditions

Conditional statements are used to test conditions in a program.

if

if age < 18 {
   ...
}
if age < 18 {
   ...
} else {
   ...
}
if gender == "male" {
   ...
} else if gender == "female" { 
   ...
} else {
   ...
}

switch

switch x {
   case 1:
      ...
   case 2:
      ...
   case hour < 12:
      ...
   default:
      ...
}

Loops

For

for i := 1; i <= 10; i++ {
}

For acting as while

x := 1
for x <= 10 {
    x++
}

Infinte for loop

x := 10
for {
   if x == 0 {
      break
   }

   fmt.Println(x)
   x--
}

Arrays

Declaring single dimension array

var ar [5]int

Initialize single dimension array

var ar = [5]int{10, 20, 30, 40, 50}

or

ar := [5]int{10, 20, 30, 40, 50}
for i := 0; i < len(ar); i++ {
   fmt.Println(ar[i])
}

Iterate using range

for i := range ar { 
    fmt.Println(ar[i]) 
}

or

for index, value := range ar {
   fmt.Println(index, value)
}

Go compiler does not allow us to create variables which we don’t use. We can ignoring index if not required by using underscore ( _ )

for _, x := range ar {
   fmt.Println(i, x)
}

String array

names := [4]string{"Ashutosh", "Archana", "Abhimanyu", "Ayushman"}
for i := 0; i < len(names); i++ {
   fmt.Println("Hello", names[i])
}

2-D array

Declare 2-D:

var ar [2][2]int

Initialize 2-D:

var ar = [2][2]int{
   {3, 5},
   {7, 9}, // This comma is necessary
}

or

ar := [2][2]int{
   {13, 15},
   {17, 19}, // This comma is necessary
}

Read 2-D array

for i := 0; i < len(ar); i++ {
   for j := 0; j < len(ar[i]); j++ {
     fmt.Printf("a[%d][%d] = %d\t", i, j, ar[i][j])
   }
   fmt.Println()
}

Functions

Functions are important components of every programming language. They allow code reusability.

A basic function

func add(a int, b int) {
   fmt.Println(a + b)
}

Use this function like this:

add(4, 5)

Function returning value

func add(a int, b int) int {
   return a+b
}

Return type is defined after () in go lang. Use this function like this:

x := add(4, 5)

Function returning multiple values

In go lang, functions can return multiple values.

func square_cube(x int) (int, int) {
   return x*x , x*x*x
}

Use this function like this:

s,c := square_cube(5)

If you do not want to receive a particular value, you can write:

_,c := square_cube(5)

Passing array to function

We can pass arrays to function like this:

func printArray(ar []int) {
   for _, x := range ar {
      fmt.Println(x)
   }
}

Use it like this:

arr := []int{10, 20, 30}
printArray(arr)

Packages

Every Go program is made up of packages. Our program starts running in package main. There are various other packages provided by Go and third party vendors.

We can use existing packages in our program like this:

import "fmt"
import "math"

or

import (
   "fmt"
   "math"
)

We create our own package by defining first line of our program like this:

package main

To install third party packages for our project, we first install the package like this:

To install mongodb package for Go

go get gopkg.in/mgo.v2

Now use this package like this:

import "gopkg.in/mgo.v2"

The init() function

When we write Go packages, we can add “init” function which will be called when the program execution starts. The init method can be used for adding initialization logic into the package.

package db
import (
    "fmt"
    "gopkg.in/mgo.v2"
)
func init {
   // initialization code here    
}