Typecasting in GoLang

Typecasting in GoLang: Typecasting means type conversion in the program. Go language does not support automatic typecasting in the program.

When the developer assigns different type values in different type variables, then typecasting is required in the go program. For example,

If there is a variable of type ‘int’ and you are going to assign a float value in it, then it will give a compile-time error in the go program. Go language is a strictly typed language, so it requires type conversion before assigning different type values.

Program:

A normal program in go language is given below; here, it requires typecasting in the program.

 package main
 // program that requires typecasting
 import "fmt"
 func main() {
             var num1 int = 223
             var num2 int = 12
             var result float32
             result = num1 / num2          // here type casting required.
             fmt.Printf("The result is : %f", result)
 } 

Output:

When you run your go file on your command prompt; containing the same code as given in the above part, then it will generate the output as below:

 # command-line-arguments
 .\type_casting.go:8:9: cannot use num1 / num2 (type int) as type float32 in assignment 

Here in the above program, the variable result is declared as float, but it performs on the ‘int’ type value, and the result will be in an integer format. In C or C++ language, it auto converts in float but in go language, and you will require to do the typecasting. The integer variable, i.e., num1 and num2, will have to convert in float before performing the action on them.

So after performing the type conversion in the above program, the renewed program will be as follows:

Program:

 package main
 // program for typecasting
 import "fmt"
 func main() {
             var num1 int = 223
             var num2 int = 12
             var result float32
             // now performing explicit type conversion...
             result = float32(num1) / float32(num2)                // type conversion performed.
             fmt.Printf("The result is : %f", result)
 } 

Output:

When you run your go file on your command prompt; containing the same code as given in the above part, then it will generate the output as below:

Typecasting in GoLang

>>> The go language also does not allow the assignment of values between different types. Neither it allows operations like addition, subtraction, multiplication, division nor does it allow assignment of values between variables having different types. It will result in an error. For example,

Program:

 package main
 // program for typecasting
 import "fmt"
 func main() {
             var num1 int = 223
             var num2 float32
             num2 = num1                        // assigning value int into float variable.
             // so, type conversion required.
             fmt.Printf("The assigned value is : %f", num2)
 } 

Output:

The above program will give the compile-time error. You will get the following result when you run the program on your command prompt:

# command-line-arguments

.\type_casting.go:8:9: cannot use num1 / num2 (type int) as type float32 in assignment

After performing the type conversion in the above program, the renewed program will be as follows:

Program:

 package main
 // program for typecasting
 import "fmt"
 func main() {
             var num1 int = 223
             var num2 float32
             num2 = float32(num1)                     // type conversion performed.
             fmt.Printf("The assigned value is : %f", num2)
 } 

Output:

When you run your go file on your command prompt; containing the same code as given in the above part, then it will generate the output as below:

Typecasting in GoLang