An Introduction to Programming in Golang

Why bother with Golang?

Compiled, statically typed language

Compilation

A compilation is a process where the source code you wrote translates into a lower-level language – the language that can be understood by a machine. Compilation can take long minutes (or even longer), but with Golang it’s different. One of the main design goals of creating GO was the speed of compilation – and they succeeded! This is good news for those of you who used to get quick feedback cycle offered by interpreted languages.

The output of this process is a single binary like C++. In terms of development, it means that the only file you push during the deployment is a binary file. Because of that fact, the whole orchestration process is not a concern. Saved time – saved money.

Static typing

Basically, what it means is that the variables must be declared a specific type (string, int, bool etc.). In Go, declaration of type goes after declaring a variable. That might be a little confusing for people who used to declare the type in C-like languages. You can declare types or leave it to the compiler.

Garbage collection

There are situations when we can easily define a variable lifetime. A local variable. Declared within a function, it disappears when the function returns. It is garbage collected. In many other cases, it is not so obvious. Without garbage collection, it is up to the developer to free the memory. For example, in C language you would need to type:void free(void *ptr).
But luckily GO has an inbuilt mechanism for garbage collection. It keeps tracking a variable and frees it as soon as it is no longer used.

Other interesting features

  • Automatic type inference (var x int = 0 or x := 0)
  • Go get – package management
  • Concurrent-friendly – goroutines and channels (async and CPU optimized usage)
  • Huge open source community
  • Cross platform
  • Strong “standard” library – you don’t need a framework
  • It is very strict what the code should look like and there are many linting tools to help

First things first

If you feel that you want to start playing with Golang right away, then I suggest you should skip this part. Open up a Go playground and start programming. Otherwise, let’s start with the basic setup.

Create a folder named ‘go’ with a src subfolder – it doesn’t matter where you put it. By its definition, GO works when placed inside a workspace. The workspace includes bon,pkg and src subfolders.

Now, if you go to download section at Golang official site, you’ll see the distributions for different platforms. Choose the right one for your system and save. Now extract this file to /usr/local with:

sudo tar -C /usr/local -xzf go1.6.3.linux-amd64.tar.gz.

Let’s set up some environment variables. The first one is GOPPATH , which points to the folder with go . The second variable is PATH. It holds information where the binary files will be founded. Edit your home/.profile and paste:

echo ‘export PATH=$PATH:/usr/local/go/bin’ >> $HOME/.profile
echo ’export GOPATH=$HOME/code’ >> $HOME/.profile/
echo ‘export GOROOT=/usr/local/go’ >> $HOME/.profile

Now update the file with:

source $HOME/.profile

You can check these system variables by typing:

echo $PATH
echo $GOPATH
echo $GOROOT

If everything goes well when you type the go version, the terminal should give you an output:

go version go1.6.3 linux/amd64

First running program

Open up the terminal and navigate to the folder created earlier:

go/scr

Create a

main.go

file:

touch main.go

Open it with your favorite text editor, IDE, vim or whatever you like and paste the code below:

Now, from the containing folder, type in the command line:

go run main.go

and the output should be:

Hello world!

This command is very useful as it compiles and runs the code. You can view the location of the temporary directory where the binary is stored. Just type:

go run --work main.go

If you would like explicitly compile and then execute code, then:

go build main.go./main

Imports

GO has a vast standard library. If you want to make use of it, you have to
import
these packages.

In the gist above we import to standard packages:
fmt and os. Remember that Golang is pretty strict with imports. Try to run something like this:

The compiler will give you two errors that fmt and os are not used. There is a good reason for that. Unused imports can slow down the process of compilation.

Variables and types

Let’s take a look at the variables declaration in the gist below:

There is one more thing to remember: Like with the unused imports, the compiler won’t compile the binary. Also, it will log an error with the unused variables specified.

Interface system

By its definition, Golang is a non-object-oriented programming language. It has structures. You don’t create objects – you create types. In OO you instantiate an object, but in GO you just create a value of a type. Let’s take a look at the examples below:

And that’s all for now. I encourage you to practice on your own and soon will invite you to the next part of Golang lessons.

Articles chosen for you:

Share the article with your friends!

Written by:

Avatar

Junior Front-end Developer. Fast learner with the ability to explain tech-related problems to the non-tech people.

Leave a comment