Different Ways to Find the Type of an Object in Golang - GeeksforGeeks

05 May, 2020
Go does not have the concept of a class type, therefore, you do not have objects in Go. You can refer any data type in Go as an object. There are several data types provided by Go such as int8, int16, int32, int64, float64, string, bool etc. There are three different ways by which you can find the type of a variable in Go at runtime.
%T in fmt package is a Go-syntax representation of the type of the value. You can use %T to find the variable type.
Syntax:
func typeofobject(x interface{}) {
    fmt.Sprintf("%T", x)
}

Example 1:
// Golang program to find the variable typepackage main  // importing required packagesimport (    "fmt")  // main functionfunc main() {    f := true    st := ""    a := 1    d := 1.0    arr := []string{"Go", "Is", "Fun"}      fmt.Printf("%T\n", f)    fmt.Printf("%T\n", st)    fmt.Printf("%T\n", a)    fmt.Printf("%T\n", d)    fmt.Printf("%T\n", arr)}
Output:
bool
string
int
float64
[]string

Example 2:
// Golang program to show how to find// the type of an objectpackage main  // importing required packagesimport (    "fmt")  func main() {    types := []interface{}{"Code", 10, true, 10.55}    for _, x := range types {        fmt.Printf("%T\n", x)    }}
Output:
string
int
bool
float64

Note: An empty interface is denoted by interface{} can be used to hold values of any type.
You can also use the reflect package which implements run-time reflection, thus allowing a program to manipulate objects with arbitrary types. reflect.TypeOf and reflect.ValueOf(x).Kind() functions help us to find the required variable type.
Syntax:
func typeofobject(x interface{}){
    fmt.Println(reflect.TypeOf(x))
}

Or
func typeofobject(x interface{}){
    fmt.Println(reflect.ValueOf(x).Kind())
}

Example 1:
// Golang program to demonstrate// the use of reflect.TypeOf functionpackage main  //importing reflect packageimport (    "fmt"    "reflect")  func main() {    f := true    st := ""    a := 1    d := 1.0    arr := []string{"Go", "Is", "Fun"}      fmt.Println(reflect.TypeOf(f))    fmt.Println(reflect.TypeOf(st))    fmt.Println(reflect.TypeOf(a))    fmt.Println(reflect.TypeOf(d))    fmt.Println(reflect.TypeOf(arr))}
Output:
bool
string
int
float64
[]string

We can do it similarly using ValueOf().Kind() function.
Example 2:
// Golang program to demonstrate// the use of reflect.ValueOf(x).Kind() functionpackage main  import (    "fmt"    "reflect")  func main() {    f := true    st := ""    a := 1    d := 1.0    arr := []string{"Coding", "In", "Go"}      fmt.Println(reflect.ValueOf(f).Kind())    fmt.Println(reflect.ValueOf(st).Kind())    fmt.Println(reflect.ValueOf(a).Kind())    fmt.Println(reflect.ValueOf(d).Kind())    fmt.Println(reflect.ValueOf(arr).Kind())}
Output:
bool
string
int
float64
slice

Example 3:
package main  // importing required packagesimport (    "fmt"    "reflect")  func main() {    types := []interface{}{"Code", true, 5, 3.14, []int{1, 2, 3, 4}}    for _, x := range types {        fmt.Println(reflect.ValueOf(x).Kind())    }}
Output:
string
bool
int
float64
slice

You can use a type switch to do several type assertions. A type switch uses several type assertions in series and runs the first matching type. In this switch, the case contains the type which is going to compare with the type present in the switch expression, and if none of the cases matches, then the default case is evaluated.
Syntax:
switch optstatement; typeswitchexpression{
case typelist 1: Statement..
case typelist 2: Statement..
...
default: Statement..
}

Example:
// Golang program to check the object type// using type switchpackage main  //importing required packagesimport (    "fmt")  func main() {    types := []interface{}{"Code", true, 5, 3.14, []int{1, 2, 3}}      for _, x := range types {        // type switch with multiple cases        switch x.(type) {        case int:            fmt.Println("int:", x)        case float64:            fmt.Println("float64:", x)        case string:            fmt.Println("string:", x)        case bool:            fmt.Println("bool:", x)        default:            fmt.Printf("data type: %T", x)        }    }}
Output:
string: Code
bool: true
int: 5
float64: 3.14
data type: []int

Picked
Go Language
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
6 Best Books to Learn Go Programming Language
strings.Replace() Function in Golang With Examples
fmt.Sprintf() Function in Golang With Examples
How to Split a String in Golang?
Inheritance in GoLang
Convert integer to string in Python
Convert string to integer in Python
Python infinity
How to set input type date in dd-mm-yyyy format using HTML ?
Matplotlib.pyplot.title() in Python