So far we have seen, how to add two numbers that are of the same type.
i.e. We have added two numbers that are of Data Type 'int'.
package main import "fmt" func main() { var x = 5 var y = 6 var z = x + y fmt.Println("The added value is :",z) }
So, if we see the above output, we got the correct added result (i.e. '11') because the Data Type of 'x' and 'y' is 'int' because both have an integer value in it.
Similarly, if we see the the variable 'z', it is also 'int'. Because 'z' holds the added value of the variables 'x' and 'y'. Which is also an integer(i.e. 11).
Now, there might also be a scenario where you want to add a floating point number and a whole number.
i.e. We want to add the whole number '5' and a floating point number '6.5'. So, what would be the output and most importantly, can we add an integer and a floating point numer in Go?
Let us see in the below example.
package main import "fmt" func main() { var x = 5 var y = 6.5 var z = x + y fmt.Println("The added value is :",z) }
Well! If we see the output! It says, invalid operation.
Which in simple words means, a floating point number (i.e. '6.5') and and a whole number (i.e. '5') cannot be added.
Now, if we see the Data Types of all the variables :
For variable 'x', it is 'int'.
For variable 'y', it is 'float'.
And that is why the numbers couldn't be added.
So, how do we solve this problem?
And this is where, 'Type Casting' comes into picture.
Let us rewrite the above program with Type Casting.
A solution to it would be, to convert the integer to float.
package main import "fmt" func main() { var x = 5 var a = float64(x) var y = 6.5 var z = a + y fmt.Println("The added value is :",z) }
Now, if we see the output,
We can see the added value of '5' and '6.5' is '11.5' and it didn't result an error.
And how did we achieve this?
Well! By Type Casting.
Now, if we see the above code, we have assigned the number '5' to a variable 'x'.
Even though we have not the specified the data type of the variable 'x' but Go makes an intelligent guess that the variable 'x' is holding a value of 'int' type.
And as we have seen that an int and a float cannot be added.
So, what we have done is, converted the variable to float and assigned it to a new variable 'a'.
The conversion to float data type is quite easy. Just place the variable 'x' inside the brackets of 'float64' data type and it will be converted to float data type.
Now, since 'a' holds the converted float data type, if we add the variables 'a' and 'y',
It happens with no error, as both 'a' and 'y' are of float types.
Now, if you don't want an additional variable 'a'. We can achieve in the below way,
package main import "fmt" func main() { var x = float64(5) var y = 6.5 var z = x + y fmt.Println("The added value is :",z) }
So, in the above code, we have directly converted the integer value (i.e. '5') to float.
And the addition proceeds without any error.
Similarly, we can convert values of any other data type to the data type you desire.
package main import "fmt" func main() { var x = 5 var y = 6.5 var a = int(y) var z = x + a fmt.Println("The added value is :",z) }
Now, if we see the output, we can see the added value of '5' and '6.5' is '11' and not '11.5'.
And the Data Type of the variable that holds '11' is 'int'.
Now, if we see the above code, we have the number '6.5' assigned to the variable 'y'.
Even though we have not the specified the data type of the variable 'x' but Go makes an intelligent guess that the variable 'y' is holding a value of 'float' type.
And as we have seen that an int and a float cannot be added.
So, what we have done is, converted the variable to int and assigned it to a new variable 'a'.
And the addition just happens fine.