本文翻译自:How to find a type of an object in Go?
How do I find the type of an object in Go? 如何在Go中找到对象的类型? In Python, I just use typeof
to fetch the type of object. 在Python中,我只使用typeof
来获取对象的类型。 Similarly in Go, is there a way to implement the same ? 同样在Go中,有没有办法实现相同的?
Here is the container from which I am iterating: 这是我正在迭代的容器:
for e := dlist.Front(); e != nil; e = e.Next() {
lines := e.Value
fmt.Printf(reflect.TypeOf(lines))
}
I am not able to get the type of the object lines in this case which is an array of strings. 在这种情况下,我无法获得对象行的类型,这是一个字符串数组。
#1楼
参考:https://stackoom.com/question/1MdD1/如何在Go中找到一个对象的类型
#2楼
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. 典型的用法是使用静态类型接口{}获取值,并通过调用返回Type的TypeOf来提取其动态类型信息。
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
Example using ValueOf(i interface{}).Kind()
: 使用ValueOf(i interface{}).Kind()
示例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
#3楼
The Go reflection package has methods for inspecting the type of variables. Go反射包具有检查变量类型的方法。
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))
}
see: http://play.golang.org/p/XQMcUVsOja to view it in action. 请参阅: http : //play.golang.org/p/XQMcUVsOja以查看它的实际效果。
More documentation here: http://golang.org/pkg/reflect/#Type 更多文档: http : //golang.org/pkg/reflect/#Type
#4楼
To get a string representation: 获取字符串表示:
From http://golang.org/pkg/fmt/ 来自http://golang.org/pkg/fmt/
%T a Go-syntax representation of the type of the value %T是值类型的Go语法表示
package main
import "fmt"
func main(){
types := []interface{} {"a",6,6.0,true}
for _,v := range types{
fmt.Printf("%T\n",v)
}
}
Outputs: 输出:
string
int
float64
bool
#5楼
Best way is using reflection concept in Google. 最好的方法是在Google中使用反射概念。 reflect.TypeOf
gives type along with the package name reflect.TypeOf
给出类型以及包名称 reflect.TypeOf().Kind()
gives underlining type reflect.TypeOf().Kind()
给出下划线类型
#6楼
I found 3 ways to return a variable's type at runtime: 我找到了三种在运行时返回变量类型的方法:
Using string formatting 使用字符串格式
func typeof(v interface{}) string {
return fmt.Sprintf("%T", v)
}
Using reflect package 使用反射包
func typeof(v interface{}) string {
return reflect.TypeOf(v).String()
}
Using type assertions 使用类型断言
func typeof(v interface{}) string {
switch v.(type) {
case int:
return "int"
case float64:
return "float64"
//... etc
default:
return "unknown"
}
}
Every method has a different best use case: 每种方法都有不同的最佳用例:
string formatting - short and low footprint (not necessary to import reflect package) 字符串格式 - 短和低占用空间(不需要导入反映包)
reflect package - when need more details about the type we have access to the full reflection capabilities 反映包 - 当需要更多有关我们可以访问全反射功能的类型的详细信息时
type assertions - allows grouping types, for example recognize all int32, int64, uint32, uint64 types as "int" type assertions - 允许分组类型,例如将所有int32,int64,uint32,uint64类型识别为“int”