golang:interface{}类型测试
在golang中空的interface即interface{}可以看作任意类型, 即C中的void *.
对interface{}进行类型测试有2种语法:
1. Comma-ok断言:
value, ok = element.(T),
其中T是具体类型.
2. Switch测试:
switch element.(type) {
case T1:
case T2:
default:
}
其中T1, T2是具体类型.
注意: element.(type)语法不能在switch外的任何逻辑里使用. 在switch外必须使用comma-ok断言或者反射:
reflect.TypeOf(element)
reflect.ValueOf(element)
golang:interface{}类型测试的更多相关文章
- golang interface类型转string等其他类型
inter 是interface类型,转化为string类型是: str := inter .(string) 转为其他类型也类似
- golang interface 类型学习
接口类型变量的内存结构 动态类型 动态值 对于动态类型指的是当其他非接口类型变量赋值给接口类型变量时,接口类型变量中的动态类型就是当前非接口类型 对于动态值指的就是当其他非接口类型变量赋值给接口类型变 ...
- 工作随笔——Golang interface 转换成其他类型
新的公司,新的氛围.一年了,打算写点什么.so,那就写google的golang语言吧. 最最最基础的语法结构见go语言菜鸟教程 接下来写点菜鸟教程没有的. go语言的设计者认为:go语言必须让程序员 ...
- Golang 的 `[]interface{}` 类型
Golang 的 []interface{} 类型 我其实不太喜欢使用 Go 语言的 interface{} 类型,一般情况下我宁愿多写几个函数:XxxInt, XxxFloat, XxxString ...
- golang interface 类型变量当作某个具体类型使用
比如,我们定义了一个 struct type person struct { Name string `json:"name"` Age int `json:"age&q ...
- golang学习笔记:Interface类型断言详情
原文链接:https://www.2cto.com/kf/201712/703563.html 1. 用于判断变量类型 demo如下: switch t := var.(type){ case str ...
- 【Go入门教程6】interface(interface类型、interface值、空interface{}、嵌入interface、反射)
interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...
- 【Go入门教程8】interface(interface类型、interface值、空interface{}、嵌入interface、反射)
interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...
- Golang基本类型整理
总是用的用的就模糊了,不知道基本的类型有哪些,看来要反反复复弄几次. Golang基本类型整理 基本类型以及定义变量需要注意的 对于基本类型的介绍,感觉这个博客讲的比较透彻,基本上都是从源码的角度来入 ...
随机推荐
- [linux]查看文件编码和编码转换
方法一:file filename 方法二:在Vim中可以直接查看文件编码 :set fileencoding 即可显示文件编码格式. 如果你只是想查看其它编码格式的文件或者想解决用Vim查看文件乱码 ...
- 学习总结 HTML简单应用
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...
- 使用JS对HTML标签进行增删改查
以下为通过JS对li标签进行简单的增删改查: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ...
- leetcode007. Reverse Integer
/* a good way to predict overflow * each time *10 must predict int overflow * not only the last time ...
- md5的一些用法
package md5; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /* * ...
- Loadrunner:安装LR11时提示缺少vc2005_sp1_with_atl_fix_redist
[问题现象] 安装LR11时提示缺少vc2005_sp1_with_atl_fix_redist: [解决办法] 手动安装缺少的组件,LR安装包中已自带该组件,为何不自动捕捉异常去获取该自带的组件去安 ...
- github的入门使用
原文 http://www.eoeandroid.com/thread-274556-1-1.html [初识Github]首先让我们大家一起喊一句“Hello Github”.YEAH!就是这样. ...
- iOS 微信支付平台集成
https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_5
- C#使用结构来传递多个参数
当参数超过5个时,建议用结构来传递多个参数. 示例代码如下: public struct MyStruct { public string str; public int number; } clas ...
- NSDateFormatter中时间格式串的含义
a: AM/PM (上午/下午) A: 0~86399999 (一天的第A微秒) c/cc: 1~7 (一周的第一天, 周天为1) ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat ( ...