Here's the TypeOf function in the reflect package:
package main import "fmt" import "reflect" func main() { num := 3 fmt.Println(reflect.TypeOf(num)) }
It is output:
int
Update:. You updated your question by indicating that you want the type to be a string. TypeOf returns a Type , which has a Name method that returns the type as a string. So
typeStr := reflect.TypeOf(num).Name()
Update 2: To be more thorough, I must indicate that you have a choice between calling Name() or String() on Type ; they are sometimes different:
vs
// String returns a string representation of the type. // The string representation may use shortened package names // (eg, base64 instead of "encoding/base64") and is not // guaranteed to be unique among types. To test for equality, // compare the Types directly. String() string
source share