Different Ways to Find the Type of Variable in Golang


Go is a statically typed programming language, which means that the type of a variable is determined at compile time. This feature allows the Go compiler to perform optimizations and improve performance. To write efficient and maintainable code in Go, it is important to know the type of a variable. In this article, we will discuss different ways to find the type of a variable in Go.

Using fmt.Printf

One way to find the type of a variable in Go is to use the Printf function from the fmt package. Printf allows us to format and print values to the standard output. To find the type of a variable, we can use the %T verb in the format string.

Example

package main

import "fmt"

func main() {
   var a int = 42
   var b float64 = 3.14
   var c string = "hello"
   
   fmt.Printf("%T\n", a)
   fmt.Printf("%T\n", b)
   fmt.Printf("%T\n", c)
}

Output

int
float64
string

Using the Reflect Package

Another way to find the type of a variable in Go is to use the reflect package. The reflect package provides a way to inspect the structure of an object at runtime. We can use the TypeOf function from the reflect package to get the type of a variable.

Example

package main

import (
   "fmt"
   "reflect"
)

func main() {
   var a int = 42
   var b float64 = 3.14
   var c string = "hello"
   
   fmt.Println(reflect.TypeOf(a))
   fmt.Println(reflect.TypeOf(b))
   fmt.Println(reflect.TypeOf(c))
}

Output

int
float64
string

Using the %v Verb

The %v verb in the fmt package can also be used to find the type of a variable. The %v verb formats the value in a default format, which includes the type information.

Example

package main

import "fmt"

func main() {
   var a int = 42
   var b float64 = 3.14
   var c string = "hello"

   fmt.Printf("%v\n", a)
   fmt.Printf("%v\n", b)
   fmt.Printf("%v\n", c)
}

Output

42
3.14
hello

Using the reflect.ValueOf Function

We can also use the reflect.ValueOf function from the reflect package to find the type of a variable. The ValueOf function returns a Value struct that represents the value of the argument. We can use the Type method of the Value struct to get the type of the variable.

Example

package main

import (
   "fmt"
   "reflect"
)

func main() {
   var a int = 42
   var b float64 = 3.14
   var c string = "hello"
   
   fmt.Println(reflect.ValueOf(a).Type())
   fmt.Println(reflect.ValueOf(b).Type())
   fmt.Println(reflect.ValueOf(c).Type())
}

Output

int
float64
string

Conclusion

In this article, we discussed different ways to find the type of a variable in Go. We saw that we can use the Printf function from the fmt package, the TypeOf function from the reflect package, the %v verb in the fmt package, and the ValueOf function from the reflect package to find the type of a variable. Knowing the type of a variable is important for writing efficient and maintainable code in Go.

Updated on: 12-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements