Type casting in Golang


January 15, 2022, Learn eTutorial
1434

In this tutorial, You are going to learn about different available conversion types in Golang. Golang supports different types of data types for variables like string, integer, boolean, float, etc. Different type of conversion leads to different results.

What is type casting in Golang?

Type conversion or type conversion is the process of converting a variable of any defined datatype to a variable of some other data type. Example converting datatypes int to float (4 to 4.0), float to int (4.0 to 4), etc.
There are two types of conversion

  1.    Implicit
    • Automatic conversion from one data type to another.
    • The data type is converted by a compiler.
    • Also known as type conversion.
  2.    Explicit
    • Manually the user explicitly performs the data type conversion.
    • The data type is converted into another by a programmer using a cast operator.
    • Also known as typecasting.
GO : Type-casting

What is the need for type conversion in Golang?

Automatic type conversion is not supported in the Go programming language. Consider the result of two numbers after performing addition which is declared of integer data type cannot be stored in a variable declared of float type. This means Golang is a strictly typed language, the variables must be converted to the required data type before assigning any value to a variable.

Golang supports explicit type conversion; the user, manually needs to convert the type of any variable.

Let us consider an example to understand the need for type conversion.
 


package main
import "fmt"

func main() {

var a  int = 25
var b int = 25
var result float32
result = a+b
fmt.Println("The result after addition is ", result)

}

Output:


_/home/0ax1ZC
./prog.go:10:8: cannot use a + b (type int) as type float32 in assignment

For the above-given code, the resultant output shows some error which conveys that the a & b of integer type cannot be assigned to float32 type variable.

GO : Type-casting

Let us see how to convert the result of type int to float so that it can be stored in the declared variable result. The code result = float32(a)+float32(b) converts to resultant output format.

Example


package main
import "fmt"
 
func main() {
 
    // taking the required data into variables
    var num1 int = 846
    var num2 int = 19
    var sum float32
 
    // explicit type conversion
    sum = float32(num1) + float32(num2)
 
    // Displaying the result
    fmt.Printf("Result = %f\n,Type = %T\n", sum,sum)
}

Output:


Result = 865.000000
,Type = float32

NOTE: Golang does not support automatic type conversion or implicit type conversion.

The syntax for type conversion in Golang?

The basic syntax followed to convert is given below


<varaiable name> := <datatype_name >(value of other type to convert)

NOTE :Go supports explicit type conversion

i.e. type conversion is carried out “automatically” by the java compiler while the “typecasting” (using a cast operator) is to be explicitly performed by the java programmer. It is usually classified into two categories
Let us understand with an example

GO : Type-casting

package main

import (
"fmt"
)

func main() {
var a int = 4 // integer number 4
f := float64(a)
fmt.Println("datatype of int type converted to float type ", f)
fmt.Printf("Type = %T\n",f)

}

Output:


datatype of int type converted to float type  4
Type = float64

In the above example, a variable of integer data type is declared with value 4 which is converted to float data type by using the above-depicted syntax.