How do I find the type of an object in Go? In Python, I just use typeof
to fetch the type of object. Similarly in Go, is there a way to implement the same ?
To be short, please use fmt.Printf("%T", var1)
or its other variants in the fmt package.
The Go reflection package has methods for inspecting the type of variables.
The following snippet will print out the reflection type of a string, integer and float.
package main
import (
"fmt"
"reflect"
)
func main() {
tst := "string"
tst2 := 10
tst3 := 1.2
fmt.Println(reflect.TypeOf(tst))
fmt.Println(reflect.TypeOf(tst2))
fmt.Println(reflect.TypeOf(tst3))
}
Output:
Hello, playground
string
int
float64
see: http://play.golang.org/p/XQMcUVsOja to view it in action.
More documentation here: http://golang.org/pkg/reflect/#Type
You can check the type of any variable/instance at runtime either using the "reflect" packages TypeOf
function or by using fmt.Printf()
:
package main
import (
"fmt"
"reflect"
)
func main() {
value1 := "Have a Good Day"
value2 := 50
value3 := 50.78
fmt.Println(reflect.TypeOf(value1 ))
fmt.Println(reflect.TypeOf(value2))
fmt.Println(reflect.TypeOf(value3))
fmt.Printf("%T",value1)
fmt.Printf("%T",value2)
fmt.Printf("%T",value3)
}
Use the reflect package:
Package reflect implements run-time reflection, allowing a program to manipulate objects with arbitrary types. The typical use is to take a value with static type interface{} and extract its dynamic type information by calling TypeOf, which returns a Type.
package main
import (
"fmt"
"reflect"
)
func main() {
b := true
s := ""
n := 1
f := 1.0
a := []string{"foo", "bar", "baz"}
fmt.Println(reflect.TypeOf(b))
fmt.Println(reflect.TypeOf(s))
fmt.Println(reflect.TypeOf(n))
fmt.Println(reflect.TypeOf(f))
fmt.Println(reflect.TypeOf(a))
}
Produces:
bool
string
int
float64
[]string
Playground
Example using ValueOf(i interface{}).Kind()
:
package main
import (
"fmt"
"reflect"
)
func main() {
b := true
s := ""
n := 1
f := 1.0
a := []string{"foo", "bar", "baz"}
fmt.Println(reflect.ValueOf(b).Kind())
fmt.Println(reflect.ValueOf(s).Kind())
fmt.Println(reflect.ValueOf(n).Kind())
fmt.Println(reflect.ValueOf(f).Kind())
fmt.Println(reflect.ValueOf(a).Index(0).Kind()) // For slices and strings
}
Produces:
bool
string
int
float64
string
Playground
If we have this variables:
var counter int = 5
var message string = "Hello"
var factor float32 = 4.2
var enabled bool = false
1: fmt.Printf %T format : to use this feature you should import "fmt"
fmt.Printf("%T \n",factor ) // factor type: float32
2: reflect.TypeOf function : to use this feature you should import "reflect"
fmt.Println(reflect.TypeOf(enabled)) // enabled type: bool
3: reflect.ValueOf(X).Kind() : to use this feature you should import "reflect"
fmt.Println(reflect.ValueOf(counter).Kind()) // counter type: int
I would stay away from the reflect. package. Instead use %T
package main
import (
"fmt"
)
func main() {
b := true
s := ""
n := 1
f := 1.0
a := []string{"foo", "bar", "baz"}
fmt.Printf("%T\n", b)
fmt.Printf("%T\n", s)
fmt.Printf("%T\n", n)
fmt.Printf("%T\n", f)
fmt.Printf("%T\n", a)
}