Home »
Golang
Different ways to find the type of a variable in Golang
Last Updated : April 19, 2025
A variable is an identifier i.e., the name of the memory blocks. A variable allows to retrieve or manipulate the value stored in memory blocks.
Each variable has its type (data type). In this tutorial, we are going to learn the different ways to find the data type of a variable.
Find the variable type using %T with fmt.Printf() function
By using the Printf() function of "fmt" package with %T format specifier, we can get the type a variable.
Syntax
fmt.Printf("%T", variable_name)
Example
// Golang program to find the variable type
// using %T with Printf() function
package main
import "fmt"
func main() {
// defining the variables
a := 10 // integer variable
b := 10.20 // float variable
c := "Hello" // string variable
d := true // boolean variable
e := []string{"India", "USA", "UK"} // string array
// printing the values
fmt.Println("a = ", a)
fmt.Println("b = ", b)
fmt.Println("c = ", c)
fmt.Println("d = ", d)
fmt.Println("e = ", e)
// printing the type of the variables
fmt.Printf("Type of a = %T\n", a)
fmt.Printf("Type of b = %T\n", b)
fmt.Printf("Type of c = %T\n", c)
fmt.Printf("Type of d = %T\n", d)
fmt.Printf("Type of e = %T\n", e)
}
Output:
a = 10
b = 10.2
c = Hello
d = true
e = [India USA UK]
Type of a = int
Type of b = float64
Type of c = string
Type of d = bool
Type of e = []string
Find the variable type using reflect.TyepfOf() function
The Typeof() function is defined in the "reflect" package and it returns the type of a variable. To use this function, we need to import the "reflect" package.
Syntax
fmt.Println(reflect.TypeOf(variable_name))
Example
// Golang program to find the variable type
// using reflect.TypeOf() function
package main
import (
"fmt"
"reflect"
)
func main() {
// defining the variables
a := 10 // integer variable
b := 10.20 // float variable
c := "Hello" // string variable
d := true // boolean variable
e := []string{"India", "USA", "UK"} // string array
// printing the values
fmt.Println("a = ", a)
fmt.Println("b = ", b)
fmt.Println("c = ", c)
fmt.Println("d = ", d)
fmt.Println("e = ", e)
// printing the type of the variables
fmt.Println("Type of a = ", reflect.TypeOf(a))
fmt.Println("Type of b = ", reflect.TypeOf(b))
fmt.Println("Type of c = ", reflect.TypeOf(c))
fmt.Println("Type of d = ", reflect.TypeOf(d))
fmt.Println("Type of e = ", reflect.TypeOf(e))
}
Output:
a = 10
b = 10.2
c = Hello
d = true
e = [India USA UK]
Type of a = int
Type of b = float64
Type of c = string
Type of d = bool
Type of e = []string
Find the variable type using reflect.ValueOf().Kind() function
The ValueOf().Kind() function is defined in the "reflect" package and it returns the type of a variable. To use this function, we need to import the "reflect" package.
Syntax
fmt.Println(reflect.ValueOf(variable_name).Kind())
Example
// Golang program to find the variable type
// using reflect.ValueOf().Kind() function
package main
import (
"fmt"
"reflect"
)
func main() {
// defining the variables
a := 10 // integer variable
b := 10.20 // float variable
c := "Hello" // string variable
d := true // boolean variable
e := []string{"India", "USA", "UK"} // string array
// printing the values
fmt.Println("a = ", a)
fmt.Println("b = ", b)
fmt.Println("c = ", c)
fmt.Println("d = ", d)
fmt.Println("e = ", e)
// printing the type of the variables
fmt.Println("Type of a = ", reflect.ValueOf(a).Kind())
fmt.Println("Type of b = ", reflect.ValueOf(b).Kind())
fmt.Println("Type of c = ", reflect.ValueOf(c).Kind())
fmt.Println("Type of d = ", reflect.ValueOf(d).Kind())
fmt.Println("Type of e = ", reflect.ValueOf(e).Kind())
}
Output:
a = 10
b = 10.2
c = Hello
d = true
e = [India USA UK]
Type of a = int
Type of b = float64
Type of c = string
Type of d = bool
Type of e = slice
Go Variable Type Detection Exercise
Select the correct option to complete each statement about identifying variable types in Go.
- The ___ package provides the
TypeOf()
function to get the type of a variable.
- The syntax
reflect.TypeOf(x)
returns a value of type ___.
- To print a variable's type using
fmt.Printf()
, use the verb ___.
Advertisement
Advertisement