GOLANG

 The Golang programming language was built to fill in the gaps of C++ and Java that Google came across while working with its servers and distributed systems.

Top reasons you should learn Go Lang programming

  • Go Programming Language has First-class functions
  • Go language has a Built-in garbage collection
  • Golang has Simple scoping rules
  • Go Programming Language has Focused vocabulary
  • Go has goroutines
  • It handles errors and bugs at the compile time which makes developers so comfortable using this language.

  • It is compiled and statically typed programming language developed by Google.
  • Statically typed means variable types are checked before by the compiler so type errors are picked up earlier.
  • It's also strongly typed language which means variable type cannot be changed after it is declared.
  • useful for writing lightweight microservices.

Pointers : It allows the programmers to directly interact with the memory allocation of different variables. And with the use of pointers, programmers can reduce the execution time of a particular program. Pointers allow easy handling of memory, arrays, structures and functions, etc.

Memory Allocation: Go programming language supports the feature of the automatic garbage collector. A running program will store objects in two memory locations, the heap and the stack. Garbage collection operates on the heap, not the stack. Generally, if a Go program has a pointer to an object then that object is stored on the heap.  As a program runs, the heap will continue to grow as objects are added unless the heap is cleaned up. 
When the programmer defines an object that gets placed on the heap, the needed amount of memory is allocated and a pointer to it is returned.

Goroutines: These are concurrent functions that run while the rest of the programs are complete. It allows multiple processes to run simultaneously and effectively.

For web apps, goroutines let you run concurrent jobs while avoiding roadblocks. The program will keep running even if the concurrent processes take longer than expected.

Only one thread in a program can be associated with thousands of Goroutines. Also if any Goroutine in the thread blocks, then another OS thread is created and the remaining Goroutines are moved to the new OS thread. All these are taken care of by the runtime

The syntax of the Go programming language is similar to the syntax of the C programming language.

Installation:    https://go.dev/dl/


Every Go file should be a part of some package.
Here we use the main package to make the file executable.
The main package requires a main function that will be called when executing the file.

func main() is the entry point of the application. Go will automatically find and execute this after execution.

we can declare all other functions inside the main function to execute them.

we can only have one main function in our entry file (main.go)








Define Variables in Go




Here Go will automatically infer the type. So you can't change the variable type in future


We can use this variable later anywhere

We use this only the first time while initializing or declaring the variable.

This is just a short-hand version. And we cannot use this shorthand outside of a function.


Printing & Formatting String


fmt -> format string and print them in console









→ new line

→ no new line

→ format string


Formatting String - Here we can put variables inside string.

%_    -> This is called format specifier

%v     -> Default format specifier

%q     -> string

%T     -> get the type of that variable

%f       -> float

%0.2f  -> around 2 decimal points

%0.1f   -> around 1 decimal point







fmt.Sprintf  -> format and save the string somewhere, doesn't prints on the console.


Slices and Arrays

Array - In go, array has fixed length. We cannot change length once declared, nor type.

var myArray [5]int = [5]int{4,6,3,8,2}


Slice - A slice is a flexible and extensible data structure to implement and manage collection of data. All the elements within a slice are of same type. A slice is a segment of dynamic array that can grow and shrink. So we can append items to slice but not in array.

var mySlice = []int{4,6,8}



Function

We can declare the function anywhere but need to call that function in main function to execute it.

To return a value, we need to specify the return type while declaring the function.

Multiple returned Values :
x, y := myFunction(2,4)
_, y := myFunction(2,4)


Naming Convention :

  • Functions with names start with an uppercase letter will be exported to another packages.

  • If the Function name starts with a lowercase letter,then it won’t be exported to another packages but still can be called in the same package


Pass by Value :

Go is known as pass by value language.

Non-Pointer Values :

  • String
  • Int
  • Float
  • Boolean
  • Array
  • Struct
When we call any variable as argument in a function in Go, it creates copy of that variable and work on that copied variable.
So in function, a copy of var is created and stored in it's own memory location.
But what if we have multiple functions working on the same variable?
Here comes the role of pointers.

Pointer Wrapper Values :
  • Maps
  • Slices
  • Functions
Here in Pointer wrapper values, when we pass these as argument in a function, it creates copy of only the pointer/ reference memory block, not the underlying data memory block.
So this new copy contains the same pointer, pointing to same underlying data so when we update any data, it changes that original data memory block.

Pointers :

Pointers are variables that contains memory address of some other variable.

  • To define pointer : 

var pName = &name

  • To get the value of pointer :

var pValue = *pName

We use pointer as an argument in function to work on the original data.

Struct :

In Go, we don't use classes, instead we create custom type using struct.
Struct is a user defined type that allow to combine items of different type into a single type.
It's like a blueprint which describes a type of data.


Receiver Function :

By using this we can associate a function to some object/struct.



Receiver functions can only be invoked by variables of the type to which they are attached to or the type which they receive.
They can not be called unlike normal functions.

functions receive a copy of the variable on which they are invoked. This means that even if it modifies the value of the variable, it will not be reflected in the original variable or in the calling function.

Interface :

An interface groups types together based on their methods.

It is necessary to implement all the methods declared in the interface for implementing an interface.

Interface is also a type.


We can use the interface in functions



Flag :

In go, flag is used to pass command line argument


Comments

Popular posts from this blog

Automation Using Python

Chat Server using UDP

NETWORK TOPOLOGY