Golang tutorial Part -1 Installation and Basics

What is Golang

Go also known as Golang is an open source, compiled and statically typed programming language created by Google.

The primary focus of Golang is to make the development of highly available and scalable web apps simple and easy.

Why Golang

Why would you choose Golang as your service side programming language when there are tonnes of other languages such as python, ruby, nodejs… that do the same job.

Here are some of the pros which I found in choosing Go

  • Concurrency is an inherent part of the language. As a result writing multithreaded programs is a piece of cake. This is achieved by Goroutines and channels which we will discuss later in the upcoming tutorials.
  • Golang is a compiled language. The source code is compiled to native binary. This is missing in interpreted languages such as JavaScript used in nodejs.
  • The language spec is pretty simple. The entire spec fits in a page and you can even use it to write your own compiler 🙂
  • The go compiler supports static linking. All the go code can be statically linked into one big fat binary and it can be deployed in cloud servers easily without worrying about dependencies.

Installation

Golang is supported on all the three platforms Mac, Windows and Linux. You can download the binary for the corresponding platform from https://golang.org/dl/

Mac OS

Download the OS X installer from https://golang.org/dl/. Double tap to start the installation. Follow the prompts and this should have Golang installed in /usr/local/go and would have also added the folder /usr/local/go/bin to your PATH environment variable.

Windows

Download the MSI installer from https://golang.org/dl/. Double tap to start the installation and follow the prompts. This will install Golang in location c:\Go and will also add the directory c:\Go\bin to your path environment variable.

Linux

Download the tar file from https://golang.org/dl/ and unzip it to /usr/local.

Add /usr/local/go/bin to the PATH environment variable. This should install go in linux.

Setting up the go workspace

Before beginning to write code, we have to setup the go workspace.

In the case of Mac or Linux, the go workspace should be located in $HOME/go. So lets go ahead and create a directory go inside $HOME.

In the case of Windows, the workspace should be located in C:\Users\YourName\go. So lets create the go directory inside C:\Users\YourName.

It is possible to use a different directory as the workspace by setting the GOPATH environment variable. But for now lets use the above location for simplicity.

All the source files for go should be located in a directory named src inside the workspace. So lets create directory src inside the go directory we created above.

Every go project should in turn have its own subdirectory inside src. Lets create a directory hello inside src to hold the hello world project.

The directory structure should look like the one below after creating the above directories.

Save the following program as helloworld.go in the hello directory we just created.

Heres what the directory structure will look like after creating the above program

Running a go program

There are a couple of different ways to run a go program. Lets look at them one by one.

1) Using go run command – Type go run workspacepath/src/hello/helloworld.go in the command prompt.

workspace path in the above command should be replaced by the path of your workspace (C:/Users/YourName/go in windows and $HOME/go in linux or Mac)

You should see the output inHello World the console.

2) Using go install command – Run commandgo install hello followed by workspacepath/bin/hello to run the program.

workspace path in the above command should be replaced by the path of your workspace (C:/Users/YourName/go in windows and $HOME/go in linux or Mac). You should see the same Hello World output in the command line.

When you type go install hello, the go tool searches for the hello package (hello is called a package, we will look into packages in more detail later) inside the workspace. Then it creates a binary named hello(hello.exe in the case of windows) inside the bin directory of the workspace. The directory structure should like below after running go install hello

3) The third cool way of running the program is using the go playground. Although this has its restrictions, this method comes in handy when we want to run simple programs. I have created a playground for the hello world program. Click here to run the program online.

You can use the go playground to share your source code with others.

A short explanation of the hello world program

Here is the hello world program we just wrote

We will see what each line of the program does in brief here. We will dwell deep into each section in the upcoming tutorials.

package main – Every go file must start with the package name statement. Packages are used to provide code compartmentalisation and reusability. Here the package name used is main

import “fmt” – The fmt package is imported and it will be used inside the main function to print text to the standard output.

func main() – The main is a special function. The program execution starts from the main function. The main function should always reside in the main package. The { and } indicate the start and end of the main function.

fmt.Println(“Hello World”) – The Println function of the fmt package is used to write text to the standard output.