Introduction to Go Programming
Go (also referred as Golang) is a procedural and statically-typed programming language having syntax similar to C language. It provides a rich standard library, garbage collection, and dynamic-typing capability.
This language also has key-value maps, length arrays features which are advance level built-in types. Go is the most powerful, performant, and scalable programming language for creating web applications, web APIs, micro-services, and other distributed services. It is one of the fastest-growing, highest-paying programming languages in America.
Evolution of Golang:
The Golang was designed by Robert Griesemer, Rob Pike, and Ken Thompson in 2007 at Google. It was launched in November 2009 as an open-source programming language. The main purpose of designing Golang was to solve the problems of other languages by keeping their useful features. In March 2012, its Version 1.0 was released. The latest version is 1.11.1 which released in 2018.
The main purpose of designing Golang was to eliminate the problems of existing languages. So we started with the problems that we are facing with Python, Java, C/C++ languages:
- Python – “Easy to use, but slow” : Python is easy but slow as it is an interpreted language that makes the code slow.
- Java – “Complex type system” : Java has a complex type system due to it additional features which makes it more complex and slow.
- C/C++ – “Complex type system as well as slow compilation time” : C/C++ was designed when the computer does not have that much space so the developer decided to optimize the compiler by using the minimal amount of space which results in sluggish compiler time.
- Also, all these languages were designed when multi-threading applications were rare, so not much effective to highly scalable, concurrent and parallel applications.
Features of Golang:
- Strong and Statically Type language: Go is Strong as well as Statically type language. Strong means once you created some variable using some data type then for the whole application it will remain type. Statically means all the variables have to define at compile time.
- Excellent Community: Golang have an excellent community for issue tracking and solution.
- Fast Compilations: Golang will includes only that packages which will import directly in the code.
- Garbage collections: Automatic garbage collection of Go is very fast.
- Built-in Concurrency: Go has the built-in feature of concurrency. Using Go Routines and channels you can handle the concurrency very easily and effectively.
Beginning with Golang Programming
Programming in Golang: Since the Golang is a lot similar to other widely used languages syntactically, it is easier to code and learn in Golang. Programs can be written in Golang in any of the widely used text editors like Notepad++, gedit etc. or on any of the text-editors. After writing the program, save the file with the extension .go or .GO.
There are various online IDEs such as The Go Playground, repl.it etc. which can be used to run Go programs without installing it. For installing Go in PCs or Laptops, we need of two software Text editor which gives you a platform where you write your source code and compiler. The text editors may be OS Edit Command, Brief, vm or vi, notepad, notepad++, etc.
Example : A simple program to print Hello Geeks!.
Explanation of the syntax of Go program:
- Line 1: It contains the package main of the program, which have overall content of the program.It is the initial point to run the program, So it is compulsory to write.
- Line 2: It contains import “fmt”, it is a preprocessor command which tells the compiler to include the files lying in the package.
- Line 3: main function, it is beginning of execution of program.
- Line 4: fmt.Println() is a standard library function to print something as a output on screen.In this, fmt package has transmitted Println method which is used to display the output.
- Comment: Comments are used for explaining code and are used in similar manner as in Java or C or C++. Compilers ignore the comment entries and does not execute them. Comments can be of single line or multiple lines.
- Single Line Comment:
- Single Line Comment:
// single line comment
- Multi line Comment:
/* multiline comment */
- Fast: Go has no Virtual Machine, it can compile directly to the machine code. Excluding Go’s intermediary assembly makes it really fast.
- Easy to learn: There is a slight change in the syntax, as it looks almost similar to other programming languages languages.
- Concurrency: It allows multiple process running simultaneously and effectively.
- Open source: It is free and open source and community is always there to help you out.
- Garbage collection: It is a key feature of go. Go excels in giving a lot of control over memory allocation and has dramatically reduced latency in the most recent versions of the garbage collector.
- Built-in testing tool: Golang has built-in testing tool which make it more rich and easy to debug.
- It has no support for generics, even if there are many discussions about it.
- The packages distributed with this programming language is quite useful but Go is not so object-oriented in the conventional sense.
- There is an absence of some libraries especially a UI toolkit.