Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - TYPE CASTING


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'.


Example :



package main
import "fmt"
    
func main() {
        
    var x = 5 
    var y = 6
    var z = x + y
    fmt.Println("The added value is :",z) 
} 


Output :



  The added value is : 11

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.


 var x = 5
 var y = 6

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.


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) 
}   


Output :



  invalid operation: x + y (mismatched types int and float64)

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.


Converting an int Data Type to an float Data Type


A solution to it would be, to convert the integer to float.


Example :



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) 
}	 


Output :



  The added value is : 11.5

Now, if we see the output,


 The added value is : 11.5

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'.


 var x = 5

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'.


 var a = float64(x)

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',


 var z = a + 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,


Example :



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) 
}	  


Output :



  The added value is : 11.5

So, in the above code, we have directly converted the integer value (i.e. '5') to float.


 var x = float64(5)

java_Collections

And the addition proceeds without any error.


 var z = x + y

Similarly, we can convert values of any other data type to the data type you desire.


Converting an float Data Type to an int Data Type


Example :



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) 
}	  


Output :



 The added value is : 11

Now, if we see the output, we can see the added value of '5' and '6.5' is '11' and not '11.5'.


 The added value is : 11

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'.


 var y = 6.5

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'.


 var a = int(y)

And the addition just happens fine.