Getting Started with Go (Part 1): An Introduction

In this series of tutorials, we are going to learn about an exciting new programming language called Go.  Go is developed and backed by Google and it’s one of the fastest rising popular languages that are available in the programmer’s arsenal. We are going to introduce the basics of Go and exploring each concept with a self contained program at the end of each tutorial to enhance your learning.

What Is Go and Why You Need It?

Go, also known by it’s longer name Golang, is a programming language that was developed at Google. It belongs to the class of languages that are compiled and statically typed. This means that unlike interpreted languages like JavaScript or Python, Go can compile your program into self-executable binary files that can run by itself.

Below are some of the reasons why Go is getting more popular:

Performance

Since your programs are compiled, it performs much faster than higher level languages such as Python and JavaScript. Go compiles your programs into lower level machine language binaries which can be executed directly on the computer similar to what C/C++ does while the language provides modern conveniences such as automatic garbage collection so that memory leaks are prevented.

Easy to Learn

Go is an easy language to learn. Unlike other programming languages which has many keywords and syntax to memorize, Go only has 25 keywords. It’s simplicity is one of the main draw factors for many programmers. One example of it’s simplicity is how it handles loops.

Compared to other languages you might be familiar with, where there are while, do .. while loops and different variations of for loops, Go only has the for loop for iterations.

However, don’t let it’s simplicity fool you, it’s for loops are much more flexible than other languages and can effective emulate while or do .. while loops, giving you a consistent and easy to remember syntax structure.

Statically Typed Language

Statically typed languages such as Go, or C#, or Java checks for variable type errors during compilation before runtime.

In dynamically typed languages, the language tries to guess what type your variable contains, such as integers or strings based on the value which you have assigned it.

A statically typed language expects you to determine the type which the variable can hold during design time. Since the variable types are defined explicitly, this means that during compilation, the compiler can check and alert you if you have accidentally assigned the wrong type of value to a variable. For example, if you have assigned an integer to a variable which you defined as only accepting strings. This is an advantage to keep your code bug free and also reduce run time errors.

Moreover, type checking is also useful during compilation as the compiler is able to optimize the performance of the software by strategically assigning where each variable is located in memory.

Who Uses It?

Go is extremely popular in the backend services due to its ease of code maintenance and high performance.

Some examples of bigger companies using Go are Uber, the ubiquitous car ride service which has converted their backend services from Python to Go for higher performance and Twitch, the live streaming video platform which implemented their video transcoding services in Go.

Or how about Medium, the very popular blogging service, which uses Go for their backend services and image processing service.

Go is gradually being adopted by more and more companies and its community is expanding everyday, giving you a great community to rely on, if you require any help.

And who else? See here.

Ok, What’s Next? How Do I Install It?

Go is open sourced and is supported on all 3 major platforms, Windows, Mac and Linux. You can always head to https://golang.org to download the latest binaries for your respective platform to get started.

Mac OS X

You can download the Mac OS installer from the Golang download page. After the image has been mounted, you can run the executable which will install Golang to /usr/local/go and the installer will add the installed folder on your path so you can execute the Go executable anywhere.

Windows

For Windows, you can conveniently download the MSI installer from the Golang download page. Start the installer and it will install the essential Go files in C:\go and will also add the directory to your path environment.

Want to Start Coding?

This tutorial would not be complete if we do not have a small working program after all the effort you had to go through for the installation of Go.

We will be writing our first Go program, a program which prints Hello World on the command line. In this tutorial, we will use a plain text editor like Notepad to create our program. In our next tutorial, we will introduce Visual Studio Code and the relevant extensions to install to ease code development with Go.

Your First Go Program

Launch your text editor such as Notepad and type the code below:

What Did We Just Typed?

In all Go programs, the starting point will always be the main() function, and we have defined it in our program as “func main()“.

We have also made use of the “fmt” or the formatting library and called a function called “Println“, also known as print line, which allowed us to print a string of text to the console window.

Ok, Let’s Run the Program!

To run the program, save the file with the extension “.go“, for example, “helloworld.go“. Then go to the command line and navigate to where you have saved your file. At the command line, type “go run (yourfilename.go)” to ask Go to compile and run the program.

You Made It!

Congratulations on making it this far!

You have learned how to install Go on your computer and how to create a simple and yet important first Go program. This knowledge forms the foundation of our later tutorials where we will build more complex command line programs that makes use of other Go’s features like structs and pointers.

CategoriesTags

Related Posts