let’s talk data types in go part 2

Lucien Makutano
3 min readAug 20, 2020

--

Go Programming Language Sticker

In the last tutorial we talked about primitive data types in go. In today’s tutorial we are going look at more advanced data types, that’s right “advanced” I just feel motivated every time I see it in a tutorial’s title 😂.

In golang we have a lot of options when it comes to choosing a collection data type.

Arrays

Arrays in go are like arrays in java or c++, are collections of elements of the same data type which means that you can not mix strings and integers or any other data type under one array unlike Python, JavaScript, PHP etc.

Arrays in golang have advantages like they are consecutive in memory thus they can be randomly accessed using indices and constant time O(1) when it comes to the retrieval of an element in that array and disadvantages like you need to know exactly how many elements will be stored upfront.

Slices

Slices are dynamic arrays. Slices like arrays hold a list of elements of the same data type. Unlike arrays, you do not need to know the number of elements you are going to store upfront because slices grow as the number of elements grows.

Something to note here is that slices are built on top of arrays, which means under the hood the golang compiler create new an array and keep add more memory space as your data grows. An other thing to note is that slices are passed by reference which means all the copies made from a slice, reference the sames underlying memory address thus modifications to a copied slice also modifies the original slice so #BeCareful.

Maps

Maps are a key => value pair collection data type. Using maps you can store a value against a key, and later use the key to retrieve that value. In this case the key could be of any primitive data type unlike arrays and slices which can only integers as the key. If you defined a key to be of a string data type you cannot change that later to be of some other data type.

Like slices, maps are also passed by reference.

Structs

structs are a bit different from the above three collection data types. Structs can store a list of different of variables of different data types which can be accessed using the “.” operator. Structs are a grouping of related data which form a schema. An example would be an Employee struct which would have a set of properties that an employee might have like firstName, lastName, salary, etc.

Since golang does not support the Object Oriented Paradigm, to create class-like structure we use structs and the best part is we can implement the inheritance-like behavior with structs and this mechanism is called fields promotion.

Unlike slices and maps, the golang’s structs are passed by value which means the modifications to the copy does not alter the original data thus memory consumption.

Conclusion

Golang has a set of data types to choose from when it comes to working with collections of elements but remember some of the collection data types are passed by reference so be careful when modifying copies. In the next article, we will explore the concept of variables in go.

Reference

go by example

structures in go

--

--