Updated April 14, 2023
Introduction to Golang Type Conversion
In Go language, type conversion is a way to change the data type of a variable. We generally change the data from initially we defined to another type of data type for example if we have defined a variable as integer and same variable we are going to use for some other purposes. if we have a variable x of int type and we are performing the division on the variable then the output may be a decimal number which is not an integer hence with help of type conversion we can change the data type of another variable which is going to hold another variable of float type.
Syntax
Below is a simple syntax for the type conversion in the go language. Here we are simply defined the variable var1 and var 2 and these variables are in the informant, which means their data type is an integer and in the next step we are dividing these variables. while performing the division we need to care that division can be any floating point number hence the data type should be float so we have taken the division calculation under the float64 and the variable which is going to hold the division output is also defined as the floating type. We can understand that outcome of two data types as the integer of var1 and var2 is a floating type by doing the type conversion of them.
Please see the below syntax for the type conversion in go language.
varint var1 var2
var k float64 = float64(var1/var2)
How Do Type Conversions Works in Go Language?
Before going in-depth of understanding the working of the type conversion the go language we need to understand why we need the type conversion. Suppose we have many numbers and we are performing some of the operations on the number and after performing those operations we are going to get the output a decimal number so here such type of case we need to change the data type of the variable which is going to hold the calculations of the variables. Some of the important points for working are given below.
- To convert types we do not need to import any package for it.
- We can write the type conversion likevar x float64 =float64(a/b), here a and b are two integer numbers and x is a floating number which means we are producing a floating-point number from two integer numbers.
- Once we use the type before the variable the compiler gets the information about the data type it is going to produce and at the same time it will expect the same data type to hole the produced output which means float64.
Examples of Golang Type Conversion
In below, we have given some of the examples where we are calculating and manipulating number which initially defined as integer, and then we are converting them into floating. We can see in the below examples where we are changing the type of two integer type to a floating point after performing any arithmetic operation them. In case if we want to execute the below examples then we can create the file with the name type.go, you are not bound to create the file with the same name you can create a file with any name and copy and paste the below examples on the file and run the command go run type.go and we can see the output of the execution.
Example #1
Code:
//Importing all the required packages to perform below tasks .
package main
import (
"fmt"
"math"
)
func main() {
//Defining the variable a, b as the integer variables
var a, b int = 4, 7
//Type conversion of the numbers after calculation of square root
var k float64 = 6+math.Sqrt(float64(a*a + b*b))
var l uint = uint(k+9)
fmt.Println("The value after conversion of the integer",a, b, l)
}
Output:
Example #2
Code:
//Importing all the required packages to perform below tasks .
package main
import (
"fmt"
"strconv"
)
func main() {
//Defining the variable as the int64 format
n1 := int64(2)
n2 := int64(3)
n3 := int64(4)
n4 := int64(5)
//Performing the type conversion for the number into binary values of them.
v1 :=strconv.FormatInt(n1, 2)
v2 :=strconv.FormatInt(n2, 2)
v3 :=strconv.FormatInt(n3, 2)
v4 :=strconv.FormatInt(n4, 2)
fmt.Println("The first value after conversion of the integer", v1)
fmt.Println("The second value after conversion of the integer", v2)
fmt.Println("The third value after conversion of the integer", v3)
fmt.Println("The fourth value after conversion of the integer", v4)
}
Output:
Example #3
Code:
//Importing all the required packages to perform below tasks .
package main
import "fmt"
func main() {
vartotalmarksint = 300
varnumberofsubjectint = 5
varaveragemarks float32
//Performing the type conversion for the average calculation of the subjects
averagemarks = float32(totalmarks) / float32(numberofsubject)
fmt.Printf("The student average marks is = %f\n", averagemarks)
}
Output:
Example #4
Code:
//Importing all the required packages to perform below tasks .
package main
import "fmt"
func main() {
//Defining the variables as the integers
var p1 int = 67
var p2 int = 71
var p3 int = 55
var p4 int = 56
var p5 int = 78
//Performing the sum of the marks and holding output in integer variable
varsumofmarksint =p1+p2+p3+p4+p5
fmt.Printf("The student Total marks is = %d\n", sumofmarks)
varaveragemarks float32
var number =5
//Performing the type conversion for the average marks calculation of the above
averagemarks = float32(sumofmarks) / float32(number)
fmt.Printf("The student average marks is = %f\n", averagemarks)
}
Output:
Conclusion
From this tutorial we learned the basic concept of the type conversion in the go language, we learned the simple syntax of the Go language and we also learned about the working of the type conversion. We focus on some of the important examples of the go language which revealed the importance of the type conversion.
Recommended Articles
This is a guide to Golang Type Conversion. Here we discuss the introduction, syntax, and working of type conversions in go language along with different examples and code implementation. You may also have a look at the following articles to learn more –