Learn to determine the data type of a variable in Go using %T and the reflect package’s TypeOf function.
In this article, you’ll learn how to determine the data type of a variable in Go. We cover two primary techniques: using the %T format specifier with fmt.Printf and using the reflect package’s TypeOf function, which can also handle literal values.
The simplest way to discover a variable’s data type in Go is by using the %T format specifier with fmt.Printf. In the example below, four variables of different types (integer, string, boolean, and float) are declared. Their values and corresponding data types are then printed.
Copy
Ask AI
package mainimport "fmt"func main() { var grades int = 42 var message string = "hello world" var isCheck bool = true var amount float32 = 5466.54 fmt.Printf("variable grades = %v is of type %T \n", grades, grades) fmt.Printf("variable message = '%v' is of type %T \n", message, message) fmt.Printf("variable isCheck = %v is of type %T \n", isCheck, isCheck) fmt.Printf("variable amount = %v is of type %T \n", amount, amount)}
To run the above program, use the following command:
Copy
Ask AI
go run main.go
The expected output will be:
Copy
Ask AI
variable grades = 42 is of type intvariable message = 'hello world' is of type stringvariable isCheck = true is of type boolvariable amount = 5466.54 is of type float32
The reflect package’s TypeOf function is a powerful alternative that works with both variables and literals. In this example, the code shows how to determine the data types of various literals.
You can also pass variables into the reflect.TypeOf function to determine their data types. The following example declares two variables and prints both their values and their types.
Copy
Ask AI
package mainimport ( "fmt" "reflect")func main() { var grades int = 42 var message string = "hello world" fmt.Printf("variable grades = %v is of type %v \n", grades, reflect.TypeOf(grades)) fmt.Printf("variable message = '%v' is of type %v \n", message, reflect.TypeOf(message))}
Execute the program with:
Copy
Ask AI
go run main.go
The expected output is:
Copy
Ask AI
variable grades = 42 is of type intvariable message = 'hello world' is of type string
Understanding the data type of variables and literals is essential when debugging or optimizing your Go programs.
That’s it for this tutorial. In this lesson, you explored two methods to determine the data type of a variable or a literal in Go using both the %T format specifier and the reflect.TypeOf function.Keep practicing and see you in the next article!