Go Lang Tutorial

Go is a light-weight programming language developed by Robert Griesemer, Rob Pike and Ken Thompson at Google in the year 2007. But it was made available to everyone in the year 2009 as an open-source programming language. It has been awarded as TIOBE's Programming Language Hall of Fame winner for 2016 and it has gained popularity ever since. Syntactic wise, it is very similar to the C language. So the syntax will be pretty straight forward with no complex abstraction. Golang mainly targets server-side web development. It is being used in many projects at Google. Go has been used to build popular products like Docker, Kubernetes, Arduino-cli, etc.

Why do we need Golang?What are its functionalities?

Despite being a statistically typed language, it is very light and agile to use. It is a fully compiled language and the speed of execution is very high too. Golang maintains a good balance of performance and simplicity. 

Want to Become a Master in Golang? Then visit here to learn Golang Certification Course from hkrtrainings.

Following are the functionalities of Golang,

Cross platform

  • It does not have any dependencies to execute the Golang code. So it will be easily executed on any platform or server

Object Oriented

  • Go is an object-oriented language but it does not support inheritance and classes. Instead, it provides structs which have properties and methods already defined

Concurrency

  • Go supports concurrency through goroutines and channels. They are easy to write by just adding a keyword of ‘go’ before a function

Multithreading

  • The goroutines in Golang are lightweight threads. They can execute simultaneously which will result in multithreading

Advantages of Golang

  • Its a relatively easy language to learn. So anyone with basic programming skills can easily master this language
  • Go has its own garbage collection, so the programmers don’t have to worry about memory management anymore
  • It has a varied set of libraries both standard & third-party and they are constantly adding new ones to help developers
    It provides concurrency and scalability
  • We can write benchmarks to test the program’s performance
  • It provides a mechanism to write unit tests in parallel with our code

Golang installation

It is very easy to set up a Golang environment on your system. All we need is a Go compiler. The latest Go installable archive file will be available at Golang Installation The latest version of the Go language as of now is 1.14.4. Choose the installable file based on your operating system.

Installation on Windows

Download the MSI installer - go1.14.4.windows-amd64.msi which will configure the environment automatically. Run the downloaded MSI file and follow the prompts to install the tools. The Go distribution will be stored at C:\Go location in your local file system. The environment variable for Golang will be automatically set to C:\Go\bin.

GOlang Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project-Based Learning

Installation on Linux

Download the package file go1.14.4.darwin-amd64.pkg and open it. Follow the prompts to install the tools. The Go distribution will be stored at /usr/local/go location in your local file system. The environment variable path would be /usr/local/go/bin.

Download the tar archive file - go1.14.4.linux-amd64.tar.gz and extract its contents to /usr/local. Create a Go tree under /usr/local/go using the below command,

tar -C /usr/local -xzf go1.14.4.linux-amd64.tar.gz

Set the environment variable by adding the below line to /etc/profile

export PATH=$PATH:/usr/local/go/bin

Note: Once the installation is done, the command prompts that are already open in your system should be restarted for the change to take effect.

How to write and execute a Go program?

First, set up a Go workspace in your system. If your OS is Linux/Mac, the workspace should be at $Home/go. If your OS is Windows, then the workspace should be created at C:\Users\YourName\go.

Open a text editor and copy the below code,

package main

import "fmt"

func main() {
fmt.Println("Hello World")
}

Save the program as helloworld.go

  • Every go program should start with the package declaration. main is the package that is used here
  • The fmt package is used for standard text output
  • The function main is the entry point for execution
  • Println is for printing text as the output on command prompt 

To run the above program, open a command prompt and execute the below command,

go run workspace path/helloworld.go

The workspace path in the above command would be the path of the workspace created earlier.

Once you run the program, Hello World will be printed on the console.

Subscribe to our youtube channel to get new updates..!

Data Types

To declare a variable in Go, we don’t have to declare its type. It will automatically assign the type based on the initialized value of the variable. 

For example, var a = 10. The variable a will be inferred with the type int

Let’s take a look at the available data types in Golang,

Numeric

  • int - represents integers
  • unit - represents unsigned integers
  • float - represents floating point numbers
  • complex - represents complex numbers which have float real and imaginary parts
  • byte - its an alias of uint8
  • rune - its an alias of int32 which represents a unicode code point
  • Bool - represents a boolean which is either true or false
  • String - a collection of bytes

Derived

  • Pointer - stores the memory address of a variable
  • Array - a collection of elements that belong to the same type
  • Slice - a wrapper on top of an array
  • Structs - represents a collection of fields
  • Function - block of code that performs a task
  • Map - to store key-value pairs
  • Interface - set of method signatures

Note: Unlike C language, Go does not allow automatic type conversion.

GOlang Training

Weekday / Weekend Batches

Conditional statements and Loops

Conditional statements

if 

  • if is a conditional statement
  • Even if we have a single line of code in if statement, braces { } are compulsory
  • We can add an optional else or else if for this statement

switch 

  • It evaluates a condition and searches for a possible match against the list specified

Loops

  • The only loop available in Golang is For loop
  • Unlike other languages, it doesn’t have any while or do-while loops 

If you have any doubts on Golang, then get them clarified from Golang Industry experts on our Golang community!

Exception Handling

Exception handling is the process of handling errors using built-in error types. It doesn’t have try/catch methods. The errors in Go are represented using the type error. If there are no errors found, it returns a value of nil.

Conclusion

Go is an easily understandable language so the developers don’t have to invest too much of their time in learning. It has a stable syntax and the code is easy to maintain too. It is mainly built to make a programmer’s life easier. So it is a language worth learning.

Find our upcoming GOlang Training Online Classes

  • Batch starts on 6th Jun 2023, Weekday batch

  • Batch starts on 10th Jun 2023, Weekend batch

  • Batch starts on 14th Jun 2023, Weekday batch

Global Promotional Image
 

Categories

Request for more information

Amani
Amani
Research Analyst
As a content writer at HKR trainings, I deliver content on various technologies. I hold my graduation degree in Information technology. I am passionate about helping people understand technology-related content through my easily digestible content. My writings include Data Science, Machine Learning, Artificial Intelligence, Python, Salesforce, Servicenow and etc.