Golang 101 : First steps in Golang

Golang 101 : First steps in Golang

ยท

6 min read

Golang

Golang is a relatively new member of the programming family but it has gained a lot of popularity because of its simplicity and many other features. It is a good choice as a language for someone who is planning or wanting to learn about DevOps but of course, is not limited to it. We will understand why it is good to learn Go (Golang), in this article along with some brief history and its architecture. So let us start our discussion.

Brief history

Go is a procedural programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language.

Before Go was developed three languages were highly used in Google namely - Python, Java, and C/C++.

All three are pretty powerful and good in their respective aspects but why Go was developed? Why did it adopt Go which is now being extensively used in and out of Google?

So, although these three languages are good and mature they had some cons which were a disadvantage for a company like Google that runs applications on a very large scale -

  1. Python is simple and easy to use along with having many good features and powerful use cases but it is an interpreted language which makes it slow for the Google standards.

  2. Java on the other hand is quick but it has a complex data type system.

  3. C/C++ is also quick but its compile time is notoriously slow and it also has a complex type system that is getting a lot of attention in the C/C++ community but still due to C/C++ legacy code they can not move past its history.

  4. Also, all these languages were developed in a time when multi-threaded applications were very rare and so the support for these was patched into these languages later and is hard to work with.

Therefore, comes into the picture 'Go' which was developed to address these issues.

How are the above issues addressed

  1. Go attempts to reduce the amount of typing in both senses of the word. Its design is such that you have to write less but get more out of it.

  2. There is no type hierarchy.

  3. The threading done by heavy languages takes more memory and slows down the process of our system. So, Go uses what is called Goroutine (which we will learn later) instead of threading which is similar to threading but consumes a lot less memory.

Architecture

There are three stages in the execution of any Golang application -

  1. The scanner stage; here the source code is scanned and tokenized into a list of tokens which is used by the parser. Tokens are just the way the compiler sees indentation, keywords like package, func, etc., variable names, and the literals. Each token is represented by its position, type, and the literal string in the source code.

  2. The parser stage; here the tokens are converted into an Abstract Syntax Tree (AST) to be used by the code generator.

  3. The code generation stage; here the AST is converted to machine code.

Note - For those who don't know what an abstract syntax tree is, in very simple terms it is just a tree representation of the source code used by the compiler as an intermediate representation for its several stages.

For full details, you can refer to this article here - How a Go program compiles down to machine code.

Features of Go

  1. Strong and statically typed; this means that type of variables are needed to be known by the compiler during compile-time and after that its type cannot be changed during runtime.

  2. Simple and easy to use and work with.

  3. Built-in concurrency feature.

  4. Garbage collected.

  5. The application compiles to stand-alone binaries with all the dependencies bundled together.

  6. Fast compile times.

  7. It has an excellent community.

  8. It is platform-independent.

Now, let's move toward writing our first Go program.

First Go Program

Folder Structure

After installation and basic configuration of Go, it is time to write our first program in Go.

The directory structure we are going to follow for this is as follows -

Screenshot from 2022-07-10 17-48-44.png

It is a good practice to mirror the folder structure similar to what it will look like in version control. You can also simply create a file and start working on it but it is not recommended and it is a bad practice. Although it is a simple application so it will not affect us as the application gets bigger than it will.

src - it is where the source code will lie. Starting from github.com is the folder structure mirrored as it will look in the version control system.

pkg - it is where intermediate binaries will be stored. For eg.- when we are integrating libraries into our application. It is done so that they don't need to be recompiled again and again.

bin - it is where all compiled binaries of our applications will be stored.

Code

Now, the code for our first simple hello world program -

package main

import "fmt"

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

To run the above we have three options but we will use the one without making any binary; run the following command in your terminal:-

go run PATH_TO_FILE

Output:-

Hello World

Code explanation

  1. package main - It tells Go which package the module is part of. main tells that it is executable.

  2. import "fmt" - Used to import packages in our applications. Here, for eg.- we are importing "fmt" which is a package for formatted I/O.

  3. func main() - It is the main function of the program from where the execution of the program takes place.

  4. fmt.Println("Hello World") - We are using the Println function of the fmt package which outputs a single line with a carriage return at the end. Here we have passed the "Hello World" string as an argument.

Wrap Up

We successfully ran our first Go program with a little background on the language ๐ŸŽ‰๏ธ๐ŸŽ‰๏ธ. There is more to learn in Go which will be more and more exciting and we will get to know why Golang is now widely used and why some of the most famous tools and software like Docker, Kubernetes, and Golang itself are written in it.

Feel Free to comment down below if there are mistakes. Also, you can connect with me on -

LinkedIn - My LinkedIn Handle

Twitter - My Twitter Handle

Github - My Github profile

ย