Different Ways to Find the Type of an Object in Golang
Last Updated :
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.
1. Using fmt for a string type description
%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:
package main
import (
"fmt"
)
func 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:
package main
import (
"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.
2. Using reflect Package
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:
package main
import (
"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:
package 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
import (
"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
3. Using type assertions
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:
package main
import (
"fmt"
)
func main() {
types := []interface{}{ "Code" , true , 5, 3.14, [] int {1, 2, 3}}
for _, x := range types {
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...