Understanding Data Structures in Golang

victor steven
Jun 24 · 7 min read

Golang has gained tremendous grounds in Software development in few years of its launch.

I don’t intend to give you the full history of the language, but go the business of the topic.

The basic data structures of Golang include:

  1. Array
  2. Slice
  3. Map
  4. Struct

1. Array

This data structure is used to store fixed number of elements. So once an array is defined, elements cannot be added or removed from the array. Note that, we can set the values for any of the index in the array.

Since Go is all about types, we define the array with the type of element it stores:

There are two ways we can define an array:

a. Defining the array to have package scope. Package scope it can be used different files that have a particular package name.

Examples:

Run this code in GoPlayground

b. Defining the array inside either the main function or any other function in your code. Such array is only accessible in the function that it is defined.

The “:=” is a short declaration operator

Run this code in GoPlayground

Since array is a sort of “limited” in certain ways, it is advisable to use slice as discussed below:

2. Slice

Slices give a more robust interface to sequences compared to arrays. Unlike an array, no need to specify the length of the slice when defining it.

We can create a slice in three way:

  • Using var: var s []int
  • Using Shorthand Notation: s := []int{1,2,3,4,5}
  • Using make( ): s := make([]int, n) n is the number of the elements

a. Package scope slices:

Observe that we didn’t define “size”

Examples:

Run this code in GoPlayground

Output:

b. Function scope slice:

Here slices are defined inside functions:

c. Slice Manipulations

We would explore how we can perform a kind of “crud” operation with slice

From the above slice example:

i. Add to a slice:

We can add elements to this slice using “append” builtin function:

We can append more than one element to the slice:

ii. Delete from a slice:

If “a” is a slice and we want to remove element at the “i” index, we do:

Say we want to remove “30” which is the third element at index “2”

iii. Replace an element with another

If “a” is a slice and we want to replace element at the “i” index, with the last element:

Say i want replace the third element now “40” (index of 2) with the last element “70”:

iv. Get particular elements from the slice

If “a” is a slice and we want to get particular elements from the slice.

To get elements j to m , we do:

We now have the slice: s =[]int{10,20,70,50,60,70}

To get the 2nd(index 1) to the 4th(index 3) element, we do:

v. Length of Slice:

The “crud” code:

Output:

Run this code in GoPlayground

d. Loop through a Slice

We can do this in two ways:

  1. Using range
  2. Using for loop

Run the code in GoPlayground

e. Nested Slice:

A slice can be nested:

Output:

Run the code in GoPlayground

3. Maps

It is possible for you to use key value pairs to refer to an element. This is accomplish using map. If you coming from a javascript, php background, see map as your object .

General syntax:

Map can be defined in three ways:

i. Using var:

The above is the general syntax that applies both in package and function scope the key is of type string, while the value of the map is of type int.

ii. Using Shorthand Notation:

iii. Using make( ):

a. Defining a simple map

Output:

Run code here: GoPlayground

b. Just like Slice, we can Add, Remove, Replace elements in a Map:

Output:

Run the code here: GoPlayground

c. Comma Ok Idiom:

From the above example , we can check if a key exist, if they do, we the found value and “ ok” to it :

d. Map with a key of string/int and value of a slice of string/int:

Example: A map with a key of string and value of a slice of string:

Run code here: GoPlayground

Output:

e. Nested Map: Map inside a Map

Run the code here: GoPlayground

Output:

4. Struct

Go struct is a collection of named fields/properties. A struct can have the same or different types of fields

A struct is defined using the “type” keyword:

Above, we defined a person struct that have three fields with their datatypes.

a. A simple Struct

Run the code here: GoPlayground

Output:

b. Struct with a slice field:

We use dot(.) to accees the individual elements in a struct:

Run the code here: GoPlayground

Output:

c. Nested Struct:

A struct can be used inside another struct as seen below:

Run code here: GoPlayground

Output:

d. Promotion:

From the example above, we can call the fields in the animal struct directly like this:

Run code here: GoPlayground

Observe that we don’t need animal to access its fields again. It means that the animal fields are now seen as herbivores fields. This is called promotion.

e. Anonymous Struct:

A struct can be defined together with its field values. The example below have a struct with map and slice fields

Run code here: GoPlayground

Output:

f. Functions with Receivers (Methods)

A function can be defined that have a receiver of type struct:

If we define a struct:

We can define a function that “receives” this struct. Lets say the function tells us if an animal can run

Run the code here: GoPlayground

Output:

From the above example, you can liken the run() function to be a field of the animal struct, because it is called just the same way a field is called (using the dot notation).

Let us cement the understanding we have about “receiver” by looking at interfaces.

g. Interfaces

Interfaces are named collections of method signatures. An interface is used to achieve polymorphism in Go.

What do i mean? An example perhaps will clarify what is said.

General Syntax:

Run the code here: GoPlayground

Output:

From the example above, we can see polymorphism happening. That is, the area() function is used for calculating the area of the circle and the square. We means the interface is implemented in the square and circle types because they both define the interface method: area()

So, that was a brief explanation of the data structures in Golang. For further reading, check the official documentation:

Golang.org

Also these:

Effective Go

Godoc.org

Thank you for taking time to read. You can drop comments if any.

victor steven

Written by

Am a software developer that specializes in Go, NodeJS, PHP, Javascript, ReactJS, Laravel, VueJS,

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade