×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Different ways to find the type of a variable in Golang

By IncludeHelp Last updated : October 05, 2024

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.

How to find the type of a variable (object) in Go?

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

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


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.