本文翻译自: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.Valuefmt.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 mainimport ("fmt""reflect"
)func main() {b := trues := ""n := 1f := 1.0a := []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() : 使用ValueOf(i interface{}).Kind()示例ValueOf(i interface{}).Kind()

package mainimport ("fmt""reflect"
)func main() {b := trues := ""n := 1f := 1.0a := []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 操场


#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 mainimport ("fmt""reflect"
)func main() {tst := "string"tst2 := 10tst3 := 1.2fmt.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"//... etcdefault: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”

如何在Go中找到一个对象的类型?相关推荐

  1. 如何在python中找到两个日期时间对象之间的时差?

    本文翻译自:How do I find the time difference between two datetime objects in python? 如何分辨两个datetime对象之间的时 ...

  2. 如何在 R 中找到 F 临界值

    当您进行 F 检验时,您将获得 F 统计量作为结果.要确定 F 检验的结果是否具有统计显着性,可以将 F 统计量与 F 临界值进行比较.如果 F 统计量大于 F 临界值,则检验结果具有统计显着性. F ...

  3. InDesign 教程:如何在 InDesign 中使用的不同类型的框架?

    欢迎观看indesign教程,小编带大家学习 InDesign 的基本工具和使用技巧,了解如何在 InDesign 中使用的不同类型的框架. InDesign 中的框架是布局的构建块.它们包含图像.文 ...

  4. rstudio中位数的公式_如何在R中找到中位数

    rstudio中位数的公式 In this tutorial, let's learn how we can find the median in R. Median is defined as th ...

  5. java整数的因式分解_如何在Java中找到整数的质数-因式分解

    java整数的因式分解 编程课程中的常见家庭作业/任务之一是关于Prime Factorization. 要求您编写一个程序以找到给定整数的素因子 . 一个数字的素数因子是将精确地除以给定数字的所有素 ...

  6. 如何在Java中找到整数的质因数–因式分解

    编程课程中的常见家庭作业/任务之一是关于Prime Factorization. 要求您编写一个程序以找到给定整数的素因子 . 一个数字的质数因子是将精确地除以给定数字的所有质数. 例如,35的素数因 ...

  7. 如何在Python中表示一个对象

    在Python中一切都是对象.如果要在Python中表示一个对象,除了定义class外还有哪些方式呢?我们今天就来盘点一下. 1.dict 字典或映射存储KV键值对,它对查找.插入和删除操作都有比较高 ...

  8. 如何在Linux中找到您的IP地址

    无论我们是否知道,我们每天都在使用Internet协议(IP). 例如,每当您在Web浏览器中键入网站名称或搜索词时,它都会查询该URL(或搜索引擎)的IP地址,然后加载该网站. 让我们将IP地址分为 ...

  9. 如何在 DAO 中找到个人自由并实现自我价值?

    转载原文链接:http://www.btcwbo.com/5643.html 组织形式的变化伴随着权力结构的调整,权力结构的调整伴随着传统编织形式的变化.如果你觉得自己像一台在公司工作的机器,没有自我 ...

最新文章

  1. 显著提升图像识别网络效率,Facebook提出IdleBlock混合组成方法
  2. java format 字符_JAVA字符串格式化-String.format()的使用
  3. 人脸特征点定位之Explicit Shape Regression
  4. 触摸屏mtp文件转c语言,F28335与上位机(触摸屏)之间的通讯遵循modbus协议使用C语言编程...
  5. canal 监听不到数据变化_数据的异构实战(二)手写迷你版同步工程
  6. Spanning-tree Potocol(整理)
  7. math.sqrt 有问题_JavaScript中带有示例的Math.sqrt()方法
  8. nonlocal和global关键字
  9. Spring Boot 返回XML
  10. 数据结构之链式队列的优化
  11. mysql 5.7.19 rpm下载_centos6.8 mysql5.7 rpm安装与完全卸载
  12. Python-Cartopy制图学习02-中国2010年5月干旱情况空间制图
  13. 多个Excel文件合并到一个Excel文件的多个工作表(Sheet)里
  14. Fedora 15不能正常关机,总是卡死在关机画面上
  15. 4.jetson更换python版本
  16. 用 Ninja and GN 来加速 C++构建
  17. 天猫精灵连接蓝牙摸索4 STM32单片机和TG7100B实现数据上发和播报
  18. 前端模块化,有这一篇就够了(上)
  19. if语句、选择语句、判断语句
  20. 微信小程序提示弹窗大全

热门文章

  1. js返回页面并刷新页面数据
  2. 内网监控利器——Nagios
  3. NAS DIY的设计和实施过程-5-Openfiler篇
  4. docker下安装mysql数据库
  5. 【NOIP校内模拟】T2 字胡串(分治)
  6. android学习-1
  7. Java Spring-Bean
  8. java 学习第三篇if判断
  9. 通过CSS调整firefox的界面。
  10. 02~ 一步一步教你使用 SVN之SVN 的介绍