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 which 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 features 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:
· More Types:
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:
If we want to import multiple packages, we can do it this way:
The best way of importing is:
- 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…read more.