Different ways to find the type of a variable in Golang

In this tutorial, we are going to learn about the different ways to find the type of a variable in Go Language?
Submitted by IncludeHelp, on March 25, 2021

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.

There are the following ways to find the type of a variable,

  1. Using %T format specifier with Printf() function of fmt package
  2. Using the TypeOf() function of reflect package
  3. Using the ValueOf().Kind() function of reflect package

1) Find the variable type using %T 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)

Program:

// 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

2) 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))

Program:

// 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

3) 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())

Program:

// 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




Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.