Skip to main content
Contact our team to know more about our services
select webform
By submitting, you acknowledge that you've read and agree to our privacy policies, and Opcito may use the information provided for business purposes.
Become a part of our team
select webform
One file only.
1.5 GB limit.
Allowed types: gif, jpg, jpeg, png, bmp, eps, tif, pict, psd, txt, rtf, html, odf, pdf, doc, docx, ppt, pptx, xls, xlsx, xml, avi, mov, mp3, mp4, ogg, wav, bz2, dmg, gz, jar, rar, sit, svg, tar, zip.
By submitting, you acknowledge that you've read and agree to our privacy policies, and Opcito may use the information provided for business purposes.
Introduction to Golang
17 Sep 2022

Introduction to Golang

Go (also known as Go language or Golang) is a general-purpose, open-source language. Golang began as Google's internal project that Google officially announced in 2009. It was developed in 2007 by Robert Griesemer, Ken Thomson, and Rob Pike. As programmers, we want languages that are simple to understand, fast to learn & use, and easy to maintain. Go is aimed at efficiency. The language comes with its own syntax, standard function, and a rich standard library. It provides all the support for a concurrency platform, has a built-in testing tool, profiling framework, and possesses easy & clear documentation.

This is the first of my two-blog series on Golang programming. Before going ahead with the features and reasons behind Golang being so popular amongst developers, let's clear some air around Golang.

Firstly, as some of us think, Golang is not only an object-oriented language. Yes, we can achieve object-oriented features using Go when needed; however, it is much more than that. The second myth is that Go is only procedure-oriented – which is also untrue. Now that we've cleared a couple of myths let's look at the key features.

Key benefits of Golang

  • Faster execution: Golang is a compiled language. It doesn't require any interpreter, leading to faster development. The boosted back-end speed of Golang helps in quicker execution.
  • Scalable: Golang offers excellent scope for scalability. Multiple functionalities can run at the same time, and it is easy to extend them to existing applications when necessary. Not only is the language itself inherently performant, but it also strengthens collaboration, readability, and maintenance.
  • Effective garbage collection: Garbage collectors play an essential role in memory management. They are responsible for tracking heap memory allocation and freeing up unused memory while keeping the allocations that are still in use. Golang uses a non-generational concurrent tri-color mark & sweep collector. The Go scheduler is responsible for scheduling the application and garbage collector. Developers need not worry about the allocation and deallocation of memory.
  • Concurrency: The key feature provided by Golang is its concurrency. Because of the concurrency approach of Golang, developers can achieve efficiency, faster execution, scalability, and manageable performance of applications. Go has introduced Goroutines - a lightweight entity to efficiently perform concurrency. Goroutines need significantly less memory to initialize and can grow & shrink depending on requirements. Also, through the channels, multiple goroutines can easily communicate with each other.
  • Dependency management: A critical job of programmers is handling the dependency of the application. Golang has a built-in package management system. This eliminates the requirement of a third-party package & dependency management system. Go 1.11 and Go 1.12 has given support for Go Modules, which manages the dependent packages in the application.
  • Active developer community: Golang is an open-source language. Currently, over 1 million developers are actively programming with Golang. Having an active and strong developer community means that support will always be available for any issues faced during the development process.

Here are a couple of reference links where you can learn and contribute:

Let's look at some fundamentals that make development in Go easy.

  • Basic components of Go:
    • Interfaces
    • Functions
    • Methods
  • More Types:
    • Struct
    • Slices
    • Maps

Go promises code efficiency, which translates into faster business software and applications. Organizations that recognized the demand for efficient and lean code adopted Go as the programming language of choice.

Writing a simple program in Golang

Programming in Go is very simple. The coding style is easy to understand and implement. Listed below are the basic Go programming structure components. We will take a closer look at each component:

  • Package declaration:

    Just as in other languages like exp. Java, where we need to define the package name first, Golang also needs to start with the declaration of the package name. <packagename>

    So, only two values can be possible for the package name string. One is the main or the name of the directory it is in.

    Package main tells the Go compiler to create an executable file.

  • Import packages:

    To import required packages in the current program, we need to import that package first. To use the data members from other packages, we need to import that package first. Following are the two ways to import package names:

    import "fmt" 

    If we want to import multiple packages, we can do it this way:

    import "fmt" 
    import  "strings" 

    The best way of importing is:

    import ( 
    "fmt" 
    "strings" 
    ) 
  • Starting point - function main()

    In Golang, the main package is a special package used with the executable programs. This package contains main() function. The main() function is a special type of function and is the entry point of the executable programs. It does not take any argument nor return anything. Go automatically calls the main() function; thus, there is no need to call the main() function explicitly. Every executable program must contain a single main package and a main() function.

  • Variables

    A Variable is the name given to a memory location to store a value of a specific type.

    There are multiple ways to declare variables in Golang, such as:

    • Declaring single variable

      var area float64
    • Declaring multiple variables of the same type

      var area, perimeter float64 // we can declare multiple variables in a single statement
    • Declaring a variable with an initial value

      var area float64 = 12.5
    • Type inference: If a variable has an initial value, Go will automatically be able to infer the type of that variable using the initial value.

      var side = 25 // Since the variable side has an initial value 25, Go can infer that it is of type int
    • Shorthand declaration: Golang provides another concise way to declare variables. This is known as shorthand declaration, and it uses: = an operator.

      side:= 25
  • Comments

    This is also very simple as other components.

     // single line comment 

    To block comments, there are two ways,

    The first is by using a set of double forward slashes and repeating them for every line.

    The second is to use opening tags (/*) and closing tags (*/).

    /* 
    anything can be commented using this block 
    */ 

    To demonstrate these fundamentals let’s write the most basic program of "Hello World"

    package main 
    import "fmt" 
    func main() { 
    // You can edit this code! 
    fmt.Println("Hello World") 
    } 

    Output:

    Hello World 

    Program exited.

This was just an introduction to Go. In my next blog, I will try to elaborate more on the programming in Go with some advanced features and coding skills. Stay tuned!

Subscribe to our feed

select webform