要使用反射比较类型,请比较 reflect.Type 值:var stringType = reflect.TypeOf("") // this can be declared at package-levelif reflect.TypeOf(v) == stringType { // v has type string}给定一个任意类型名称X,您可以使用以下方法构造该类型:var xType = reflect.TypeOf((*X)(nil)).Elem()if reflect.TypeOf(v) == xType { // v has type X}如果你想检查一个值是否是某种类型,那么使用类型断言:if _, ok := v.(string); ok { // v is a string}如果要将类型映射到字符串,请使用由 reflect.Type 键入的映射:var typeName = map[reflect.Type]string{ reflect.TypeOf((*int)(nil)).Elem(): "int", reflect.TypeOf((*string)(nil)).Elem(): "string", reflect.TypeOf((*F)(nil)).Elem(): "F",}...if n, ok := typeName[reflect.TypeOf(f)]; ok { fmt.Println(n)} else { fmt.Println("other")}