Mutable and Immutable Data Types with Golang


Introduction from 100 days of golang (15 Part Series)

In this post of the 100 days of GOlang Series, you will be understanding the mutable and immutable data types in Golang. Firstly, you will understand the concept of mutability and understand the differences in mutable and immutable data types, further you will explore which data types in Golang are Mutable and Immutable.

Mutable Data Type with Golang

The mutable data type is a data type that can be modified without reallocating any chunk of the memory assigned at the time of initialization. In simple words, a variable is mutable if its value can be altered without reallocating itself to a new memory space.

The value located in a memory address can be modified in mutable data types. This means we do not have to reallocate any memory or change the pointer of a variable to point to another address in order to change the value of the variable.

Golang Immutable Data Types

The immutable data type is a data type that cannot be modified without allocating new memory. So, the immutable data type has to reallocate memory for making changes to the value of a variable. This might be a downside if the variable is holding a large set of values, it will require a lot of memory re-allocation for a slight change in the value.

Immutable data types also mean that you cannot change the value in the memory address to which the variable is pointing, but you can make the variable point to a different memory location under the hood to change or modify the content of a variable.