GO类型查询
来源:互联网 发布:看电影用什么软件最好 编辑:程序博客网 时间:2024/06/06 14:07
A type switch compares types rather than values. It is otherwise similar to an expression switch. It is marked by a special switch expression that has the form of a type assertion using the reserved word type
rather than an actual type:
switch x.(type) {// cases}Cases then match actual types
T
against the dynamic type of the expression x
. As with type assertions, x
must be of interface type, and each non-interface type T
listed in a case must implement the type of x
. The types listed in the cases of a type switch must all be different.The type in a case may be nil
; that case is used when x is a nil
interface value. There may be at most one nil
case.
Given an expression x
of type interface{}
, the following type switch:
switch i := x.(type) {case nil:printString("x is nil") // type of i is type of x (interface{})case int:printInt(i) // type of i is intcase float64:printFloat64(i) // type of i is float64case func(int) float64:printFunction(i) // type of i is func(int) float64case bool, string:printString("type is bool or string") // type of i is type of x (interface{})default:printString("don't know the type") // type of i is type of x (interface{})}could be rewritten:
v := x // x is evaluated exactly onceif v == nil {i := v // type of i is type of x (interface{})printString("x is nil")} else if i, isInt := v.(int); isInt {printInt(i) // type of i is int} else if i, isFloat64 := v.(float64); isFloat64 {printFloat64(i) // type of i is float64} else if i, isFunc := v.(func(int) float64); isFunc {printFunction(i) // type of i is func(int) float64} else {_, isBool := v.(bool)_, isString := v.(string)if isBool || isString {i := v // type of i is type of x (interface{})printString("type is bool or string")} else {i := v // type of i is type of x (interface{})printString("don't know the type")}}注意:The "fallthrough" statement is not permitted in a type switch.
0 0
- GO类型查询
- Go获取类型底层类型
- go - 内置基础类型
- go - struct类型
- Go-类型断言
- go struct 类型
- go 类型、变量、常量
- Go语言变量类型
- go语言学习-类型
- go类型断言
- go 类型练习
- go布尔bool类型
- go语言基本类型
- Go语言程序设计:集合类型
- go 类型转换
- Go语言-类型
- go函数类型讲解
- GO类型断言
- Redis 学习笔记(六)之 set无序集合
- 机器人操作系统ROS Indigo 入门学习(7)——理解ROS Topic
- 【图像处理】IPTC Tags
- SQL中显示查询结果的前几条记录
- leetcode
- GO类型查询
- PHP调用系统函数时报错未定义解决方法
- 上传图片,头像
- struts中的全局结果页面配置
- mysql性能优化(九) mysql慢查询分析、优化索引和配置
- leetcode
- 目标检测--Training Region-based Object Detectors with Online Hard Example Mining
- xutils3解析数据:
- CCF CSP 网络延时 树的直径