Advanced Data Types in Go
Introduction
In my previous blog, I discussed the basic data types in Go. In this blog, we will be going a step ahead and learning about advanced data types in GO.We will be talking about
- Arrays
- Slices
- Maps
- Structs
Arrays
Arrays as in other traditional languages are a collection of variables of homogeneous types i.e means instead of creating 10 variables to store same type of data let's say marks we create an array that can store 10 elements.
Some of the key points about arrays in Go are:
- Arrays in Go are fixed size
- Array index starts from 0
- Same type of data can be stored in an array
- Data Type has to be given while the array declaration
The syntax for array declaration:
We can also use shorthand notation for the Arrays
Slices
Slices, on the other hand, is a data structure more flexible than arrays. They are similar to arrays in the sense that they are used to store homogeneous data. They basically refer to the underlying array with some added flexibility.
A slice stores 3 things:
- Reference to the underlying array
- Length
- Capacity
A slice can be created in 2 ways:
- Using a slice operator on an array
- Creating a slice manually
Using Slice operator on an array
This will create a slice s1 which will be having elements arr1[0],arr1[1].
Elements of a slice can be accessed in the same way we access an array
Creating a slice manually
Creating a slice is no different from creating an array, the only difference between creating an array is that we don't have to give the sizes.
To get the length and capacity of this slice we can use len() and cap() functions
Capacity of a slice is the maximum number of elements that can be stored in a slice, yes you heard it right a slice can be extended, that is one of the main difference between an array and a slice and the reason slices are extensively used in Go.
We can use inbuilt make function to create a slice with different capacity
Here we have created a slice that can store 3 elements but can be extended for up to 5 elements.
Maps
Maps is a data structure similar to hash maps in other languages. It is used to store key-value pairs.
This creates a nil map in which no key can be added, this is just a nil map. As per official documentation
"Map types are reference types, like pointers or slices, and so the value of
m
above is nil
; it doesn't point to an initialized map. A nil map behaves like an empty map when reading, but attempts to write to a nil map will cause a runtime panic; don't do that."
To initialize a map we can use built-in function make
We can access, delete and count the number of keys in a map using the following function
We can also check if the key exists or not in a map using the accessor statement in the follwoing ways
the variable ok will be true if the key is present else false.
Structures
Structures are data structures to implement data encapsulation in Go. A structure is collection of heterogeneous data.
A structure can be declared as follows:
This creates a structure name person with 2 variables
- name of type string
- age of type int
This will not create any variable just declaration of a struct to create a variable of type person and access its variable we need to initialize a variable of type person
With this, we end this blog on some advanced data types in GO.
If you missed the basic data types go through my previous blog on basic data types.
Next, I will be writing about receivers and concepts on encapsulation in Go.
Comments
Post a Comment