golang获取变量或对象类型的几种方式总结
Byte-Coder 人气:0fmt.Printf("%T")方式
示例:
var1 := "hello world" fmt.Printf("var1 = %T\n", var1)
这个是最简单,也是最推荐的在at rumtime时获取变量类型的方式,
用fmt.Printf("%T")实现返回变量类型的函数
由此衍生出可复用获取变量或者对象type的函数,如下面函数,直接返回变量类型
func typeofVariable(variable interface{}) string { return fmt.Sprintf("%T", variable) }
reflect.TypeOf方式
示例:
var1 := "hello world" fmt.Printf("var1: %s\n", reflect.TypeOf(var1))
用 reflect包里的函数 TypeOf()来实现,用起来相对来说复杂些,如果只是单纯地想在runtime时获取一下变量类型,还是推荐第一种方式
用reflect.TypeOf实现返回变量类型的函数
由此衍生出可复用获取变量或对象type的函数,如下面函数,直接返回变量类型
func typeofVariable(variable interface{}) string { return reflect.TypeOf(variable).String() }
reflect.ValueOf.Kind()方式
示例:
var1 := "hello world" fmt.Println(reflect.ValueOf(var1).Kind())
用 reflect.ValueOf.Kind()实现返回变量类型的函数
func typeofVariable(variable interface{}) string { return reflect.ValueOf(variable).Kind().String() }
断言方式
func typeofObject(variable interface{}) string { switch variable.(type) { case string: return "string" case int: return "int" case float32: return "float32" case float64: return "float64" case bool: return "boolean" case []string: return "[]string" case complex128: return "complex128" case complex64: return "complex64" case map[string]float64: return "map" case Employee: return "Employee" default: return "unknown" } }
代码示例:
package main import ( "fmt" "reflect" ) type Employee struct { name string age int salary float64 } // 4 ways to return a variable's type at runtime func typeofObject(variable interface{}) string { switch variable.(type) { case string: return "string" case int: return "int" case float32: return "float32" case float64: return "float64" case bool: return "boolean" case []string: return "[]string" case complex128: return "complex128" case complex64: return "complex64" case map[string]float64: return "map" case Employee: return "Employee" default: return "unknown" } } func main() { var1 := "hello world" var2 := 10 var3 := 2.55 var4 := []string{"BeiJing", "ShangHai", "ShenZhen"} var5 := map[string]float64{"BeiJing": 3.2, "ShaiHai": 1.2} var6 := complex(3,4) var7 := true var8 := Employee{"Sam",30,15000.5} fmt.Println("############### Using %T with Printf ########################") fmt.Printf("var1 = %T\n", var1) fmt.Printf("var2 = %T\n", var2) fmt.Printf("var3 = %T\n", var3) fmt.Printf("var4 = %T\n", var4) fmt.Printf("var5 = %T\n", var5) fmt.Printf("var6 = %T\n", var6) fmt.Printf("var7 = %T\n", var7) fmt.Printf("var8 = %T\n", var8) fmt.Println("###############Using reflect.TypeOf Function #######################") fmt.Printf("var1: %s\n", reflect.TypeOf(var1)) fmt.Printf("var2: %s\n", reflect.TypeOf(var2)) fmt.Printf("var3: %s\n", reflect.TypeOf(var3)) fmt.Printf("var4: %s\n", reflect.TypeOf(var4)) fmt.Printf("var5: %s\n", reflect.TypeOf(var5)) fmt.Printf("var6: %s\n", reflect.TypeOf(var6)) fmt.Printf("var7: %s\n", reflect.TypeOf(var7)) fmt.Printf("var8: %s\n", reflect.TypeOf(var8)) fmt.Println("###############Using reflect.ValueOf.Kind() ########################") fmt.Println(reflect.ValueOf(var1).Kind()) fmt.Println(reflect.ValueOf(var2).Kind()) fmt.Println(reflect.ValueOf(var3).Kind()) fmt.Println(reflect.ValueOf(var4).Kind()) fmt.Println(reflect.ValueOf(var5).Kind()) fmt.Println(reflect.ValueOf(var6).Kind()) fmt.Println(reflect.ValueOf(var7).Kind()) fmt.Println(reflect.ValueOf(var8).Kind()) fmt.Println("################## Using Type assertions ###########################") fmt.Println(typeofObject(var1)) fmt.Println(typeofObject(var2)) fmt.Println(typeofObject(var3)) fmt.Println(typeofObject(var4)) fmt.Println(typeofObject(var5)) fmt.Println(typeofObject(var6)) fmt.Println(typeofObject(var7)) fmt.Println(typeofObject(var8)) } /* 几个复用的函数来判断变量的类型at runtime // Using %T func typeofVariable(variable interface{}) string { return fmt.Sprintf("%T", variable) } // Using reflect.TypeOf() func typeofVariable(variable interface{}) string { return reflect.TypeOf(variable).String() } // Using reflect.ValueOf().Kind() func typeofVariable(variable interface{}) string { return reflect.ValueOf(variable).Kind().String() } */
结果演示:
总结
加载全部内容