Golang for Beginners: A Simple Guide to Understanding Basic Go

Golang for Beginners: A Simple Guide to Understanding Basic Go

We are living in an era where technology continues to emerge at a breakneck speed. As 21st century developers, it is crucial for us to learn new coding languages to keep pace with this rapid change as it increases our versatility, widens our competencies and brightens our career prospects.

But that being said, what is perhaps most critical in the role of a developer is not amassing as many languages as you can. Instead, it’s actually all about being able to look at a problem and using the relevant skills under your belt to solve it. When we have to consider factors such as application performance, usability, and efficiency, this is where your added knowledge comes in.

After spending some time developing in JavaScript and TypeScript, I felt that it was a natural step to broaden my horizons and decided to pick up Go. Coincidentally, having the opportunity to use it at work also gave me the extra impetus to explore this new tool and share about my experience.

Before we get started, a disclaimer: This article is not meant to be exhaustive and does not comprehensively cover the full spectrum of Go. If you’re interested in taking a deeper dive into Go, you can find some resources at the end of this article.

Without further ado, let’s begin with a quick initiation!

Overview of Go

Go is a strong and statically typed programming language which essentially means each variable has a type which cannot be changed overtime and must be defined at compile time. This would help us to capture errors easily at compilation time.

As Go is developed by Google, we are assured of a strong community supporting this language. In terms of pull requests trends (based on statistics), Go has been steadily climbing up the popularity charts and has reached the top 5 in just four years.

According to Go history, the Google team had to choose between efficient compilation, efficient execution or ease of programming. All three were not available in any mainstream language. Hence, Go was created by taking what was best from other languages and designed for practicality. The merits include (again not exhaustive):

  1. Making Go simple and concise: Go has not experienced much change in the last decade. This allows programmers to easily learn the Go syntax as it isn’t too dissimilar to past releases. Simplicity also limits the number of ways the code is being written so it’s always easy to comprehend.
  2. Ability to run Go code quickly: Compilation is rather fast since Go is designed to allow the compiler to quickly compile dependencies.
  3. Garbage collection: This comes free with Go and removes memory instances that are no longer in use. Go is also efficient as it takes care of the memory type which decides if a Heap or Stack should be allocated to the structure.

Interesting Slices of Go

While learning Go, I came across some interesting slices (pun intended) which behaved differently from JavaScript. Now that we have a basic introduction, let’s take a look what will be covered in this article:

  1. Zero values in Go
  2. Go infers the type of initialised variables and initialises them in multiple ways
  3. Multiple return values and Named return parameters
  4. Exported & unexported names in Go
  5. Composite types (Arrays, Slices, Maps)
  6. Interfaces
  7. Concept of pointers

1. Zero values in Go

Unlike in JavaScript, where declared and uninitialised variables are usually given a value of “undefined”, variables in Go declared without an explicit initialised value will be given a default zero value.


2. Go infers the type of initialised variables and initialises them in multiple ways

a. Go can infer the data type when given a value assignment even though it is a statically typed language. Hence, we do not need to specify the type at the time of declaration. (Note: I’m using the package reflected here to determine the type of the value)

Run an example here.


b. Go variables can be initialised in multiple ways:

Run an example here.


3. Multiple return values and named return parameters

a. A function can return any number of results. It is usually the case for a function to return both result and error values.

Run an example here.


b. Named return parameters

We are allowed to explicitly name the return variables in the Go functions definition, thus eliminating the need to mention the variables name again with the return statement. This helps with readability as one can learn about the return parameters when reading the function signature. This is only recommended for short function signature.

Run an example here.


4. Exported and Unexported Names in Go

When we import a package, we can only access its exported names. Usually in the form of type, function or variable, an exported name must start with a capital letter in order to be visible outside the package. Otherwise, it will NOT BE accessible outside the package.

Run an example here.


5. Composite types

  • Array

In Go, an array is an ordered sequence of the same typed elements with a specific length defined at creation time. When an array has allocated its size, we can no longer change them.

Run an example here.


  • Slice

Built on arrays, a slice has an index and length. Unlike arrays, they are dynamically sized and therefore more flexible. Slice is closer to what we know as an array in JavaScript.

Run an example here.


  • Struct

Structs are a collection of mutable, user-defined typed fields. I would like to think of them as object templates for a data record (e.g. different attributes to represent employee profile).

Run an example here.


6. Interfaces

An interface in Go is a type collection specified with a set of methods. We can think of it as defining a set of behaviours for similar types of objects and viewing the interface in a way to achieve polymorphism.

For instance, an interface for an Employee would likely have a method called jobTitle.


Now that we have an Employee interface, let’s take a look at an example of a certain type of employee. Imagine we have different job titles for employees in different departments (we call them hives in PALO IT). The digitalhive and agilehive types would have satisfied the Employee interface since they are made up of employees with different job titles.

As we want different types (digitalhive and agilehive) to behave in the same manner with a common method called jobTitle, we then use the jobTitle method independent of their types with interface.

Run an example here.



7. The concept of Pointers

To understand pointers, we need to first understand what happens when we assign data to a variable. When data is stored, we store it in memory at a particular address. The address of a memory address is represented by a hexadecimal. To access the stored data, we need to know this address. A variable can then give us the memory location where the data is stored. Now, a pointer is also a kind of special variable but it stores the memory address of another variable.

Run an example here.


Now that we understand how pointers work and what they are, when do we use pointers?

Since struct is a pass by value type, I will use it to explain the use of pointers. When structs are passed into functions by value, it simply means a copy of a struct will be made when we pass it into a function. This applies to the rest of the pass by value types as well.

Normally, we need to pass in a struct by reference whenever we want to make changes to the copy that we are passing in. (Note: In general, some Go guidelines suggest passing small structs as values unless the function we are calling needs to modify them)

Here are some example of values types where we would typically use a pointer to change things when passing in a function:


A Retrospective

To wrap it up, learning new languages can help developers improve their problem-solving skills as it expands your thought processes. It is always beneficial to be familiar with new tools since different application problems may sometimes require a fresh approach. Having said that, I hope that by sharing some of the Go slices I found interesting and different from JavaScript, you will be encouraged to pick it up as well.

In my opinion, the learning curve for Go is perhaps higher and more demanding since it is vastly different from JavaScript. However, the potential benefits it can bring are immense. For instance, since Google built Go to solve scalability issues, it will greatly assist development teams who deal with a predictably growing volume of activities and heavy server requests. As Go has support for concurrency (Goroutines), your teams can work on multiple things simultaneously and independently.

There is plenty of readily available information out there to help you get started with Go. (A TODO application is a great way to start building and applying your knowledge).

These are some of the resources that have helped me greatly:

Go Documentation

Go By Example

Learn Go Programming — Go Tutorial for Beginners

Go Playground — For testing proof of concept codes

Related articles

Metaverse - taking business to new realities
3 mins
Tech trends
Metaverse - taking business to new realities
Is Web3 Part Of Your Customer Acquisition Strategy?
5 mins
Tech trends
Is Web3 Part Of Your Customer Acquisition Strategy?
Unlocking the Power of Data: Part 2 - An Overview of Data Strategy
7 mins
Tech trends
Unlocking the Power of Data: Part 2 - An Overview of Data Strategy

Button / CloseCreated with Sketch.