Go is strongly typed language. That means it will not accept other types when variable is declared with different type. Data type conversion comes very useful in order to perform certain operation on the data.
How to check data type in Go?
Before we get into modifying the data type, let’s see how we can verify data types in Go.
Using Format Specifier
Go provides format specifier %T to get the data type of the variable. This can be useful for debugging certain errors.
1package main
2
3import "fmt"
4
5func main() {
6 var grades int = 12
7 var isHighest bool = true
8 fmt.Printf("Data type of grades: %T\n", grades)
9 fmt.Printf("Data type of isHighest: %T\n", isHighest)
10}
Data type of grades: int
Data type of isHighest: bool
Using reflect.TypeOf function
There is a built-in function for checking the type of the variable. This function TypeOf is located in reflect module. So, we will have to import this module before using this function.
1package main
2
3import (
4 "fmt"
5 "reflect"
6)
7
8func main() {
9 var str string = "hello there"
10 var amount float32 = 23.23
11
12 fmt.Printf("Type: %v \n", reflect.TypeOf(str))
13 fmt.Printf("Type: %v\n", reflect.TypeOf(amount))
14}
Type: string
Type: float32
Another important point to note with above code is that when we want to import multiple modules, we can put them in parentheses.
1import (
2 "fmt"
3 "reflect"
4)
Type conversion (Type casting)
The processing of converting a value of one type to another is called type casting. The easiest way to convert to different type is to wrap the variable with the type we want to convert to.
Data types can be converted to different type, but it can also result in loss of some precision. When we convert wider type into narrow type, that will result in precision loss. For example, converting
floattointwould result in some precision loss.
1package main
2
3import (
4 "fmt"
5)
6
7func main() {
8 var i int = 25
9 var convertedInt float64 = float64(i)
10 fmt.Printf("%.2f\n", convertedInt)
11
12 var f float64 = 39.99
13 var convertedFloat int = int(f) // truncates the number, removes the decimal point values
14 fmt.Printf("%d\n", convertedFloat)
15}
25.00
39
If we try to convert incompatible types from one type to another, it will result in compilation error.
Below code throws an error as Go compiler doesn’t know how to convert word hello to integer.
1package main
2
3import "fmt"
4
5func main() {
6 var s string = "hello"
7 var strToInt int = int(s)
8 fmt.Printf("%d\n", strToInt)
cannot convert s (variable of type string) to type int
For conversion between string and numeric data types, we have strconv module. This is used to convert string type into integer and vice a versa. This module provide a function strconv.Itoa to convert integer into string value and strconv.Atoi to convert string to integer values. This module also provides various functions to parse different types of values from string like ParseInt, ParseBool, ParseFloat etc.
The function Itoa takes a number and returns string representation of the number. Here, we’ve used %q format specifier so that it prints string in double quotes to make it clear that this is string.
The function Atoi takes a string and return two values. First is converted number if successful, but if unsuccessful, second return value will have the error message for why it failed.
1package main
2
3import (
4 "fmt"
5 "strconv"
6)
7
8func main() {
9 var num int = 10
10 str := strconv.Itoa(num)
11 fmt.Printf("%q\n", str)
12
13 str = "200"
14 value, err := strconv.Atoi(str)
15 fmt.Printf("Value: %d, err: %v\n", value, err)
16}
"10"
Value: 200, err: <nil>
Conclusion
This lesson started with how we can check the data type of a variable in Golang. Go provides %T format specifier as well as reflect.TypeOf method from reflect module to do this. Then, we also looked at how we can convert data types between numeric values using simply the type we want to convert to as a function. For string, we had to use special functions from strconv module. These were Atoi for String to Integer and Itoa for Integer to String conversion.


Comments