Golang for Beginners: A Simple Guide to Understanding Basic Go
Koh Leok Si
5 mins read
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.
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):
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.
Ability to run Go code quickly: Compilation is rather fast since Go is designed to allow the compiler to quickly compile dependencies.
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
Zero values in Go
Go infers the type of initialised variables and initialises them in multiple ways
Multiple return values and Named return parameters
Exported & unexported names in Go
Composite types (Arrays, Slices, Maps)
Concept of pointers
1. Zero values in Go
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)
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.
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.
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.
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.
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:
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: