Zero Values and Complex Data Types In Golang
In the last blog, we talked about the Primitive Data Types of Golang. If you haven’t read it yet, first go read that blog. In that blog, in the end, we talked that we will learn about zero values and how we will optimize our code. So that’s the topic of this blog.
In the photo above, you can see that initially, when we are declaring the variables, we are not assigning the values at the time of declaring the variables.
Now, let’s look at what will be the output if we print the value of our variables before assigning values to them.
In the terminal, you may notice that we are only seeing two zeros. But in reality, we are looking at two zeros and two empty strings""
. A question may arise in your mind that if we haven’t assigned any value to those variables, how can it decide on its own the values of the variables.
This is because whenever we declare a variable without assigning values to it, the Go language does that for us by automatically assigning Zero Values or Default Values. Each variable is given a default value as per its type.
Default values corresponding to their types are given below:
false
for booleans, 0
for numeric types, ""
for strings, and nil
for pointers, functions, interfaces, slices, channels, and maps.
Now let’s move forward towards optimizing our code. In this blog, we will look at one of the Complex Data types present in Go. We will be talking about type Struct.
A struct in Go is similar to classes if you are coming from OOP paradigm languages like C++, Java, Python, etc.
What it basically does is it encapsulates different types which we use to describe a particular thing and form a single entity that can represent all the features of that particular thing.
For example, in our code above, we are declaring and assigning values to different variables only to describe a student. What we can do is define a student of type Struct and populate it with all the features we want the student to have. Let’s understand it more clearly by writing the code and then understanding what we have written.
Note that in the code above, we are not declaring a struct with keyword var, instead, we are using another keyword present in Golang called type. The keyword type is used when we want to define our own type apart from those present in the language.
Here, we are defining a type student of type struct. Confusing right. Let’s frame the sentence more clearly. We are declaring a variable of type student whose underlying type is a struct which allows us to accumulate different data types inside it. It will get more clear as we move further ahead in the course.
Since now we can define structs in Golang, I want you to create a solution for the following problem.
Problem #2: We Define A Car With The Following Characteristics.
No. of tires.
Driving fuel. (i.e. Petrol, Diesel, Electric, etc.)
Capacity. (2-seater, 4-seater, etc.)
Company Name
Car Name
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Try to solve the above problem and let me know your answers in the response section.
In the next blog, we will look at more complex data types.