在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{}类型测试的更多相关文章

  1. golang interface类型转string等其他类型

    inter 是interface类型,转化为string类型是: str := inter .(string) 转为其他类型也类似

  2. golang interface 类型学习

    接口类型变量的内存结构 动态类型 动态值 对于动态类型指的是当其他非接口类型变量赋值给接口类型变量时,接口类型变量中的动态类型就是当前非接口类型 对于动态值指的就是当其他非接口类型变量赋值给接口类型变 ...

  3. 工作随笔——Golang interface 转换成其他类型

    新的公司,新的氛围.一年了,打算写点什么.so,那就写google的golang语言吧. 最最最基础的语法结构见go语言菜鸟教程 接下来写点菜鸟教程没有的. go语言的设计者认为:go语言必须让程序员 ...

  4. Golang 的 `[]interface{}` 类型

    Golang 的 []interface{} 类型 我其实不太喜欢使用 Go 语言的 interface{} 类型,一般情况下我宁愿多写几个函数:XxxInt, XxxFloat, XxxString ...

  5. golang interface 类型变量当作某个具体类型使用

    比如,我们定义了一个 struct type person struct { Name string `json:"name"` Age int `json:"age&q ...

  6. golang学习笔记:Interface类型断言详情

    原文链接:https://www.2cto.com/kf/201712/703563.html 1. 用于判断变量类型 demo如下: switch t := var.(type){ case str ...

  7. 【Go入门教程6】interface(interface类型、interface值、空interface{}、嵌入interface、反射)

    interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...

  8. 【Go入门教程8】interface(interface类型、interface值、空interface{}、嵌入interface、反射)

    interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...

  9. Golang基本类型整理

    总是用的用的就模糊了,不知道基本的类型有哪些,看来要反反复复弄几次. Golang基本类型整理 基本类型以及定义变量需要注意的 对于基本类型的介绍,感觉这个博客讲的比较透彻,基本上都是从源码的角度来入 ...

随机推荐

  1. oracle 大字段clob检索

     select * from tab_name twhere dbms_lob.instr(t.daziduan, 'aaa', 1, 1) > 0; 

  2. Linux选型:开源不是免费 首选红帽和SUSE

    首发:http://tech.it168.com/a2014/0324/1606/000001606245.shtml 企业级服务器系统选型报告:http://www.it168.com/redian ...

  3. Chrome调试(debugger)总是进入paused in debugger状态

    在通过Chrome浏览器进行web前端开发时,我们会经常用到Chrome自带的debugger工具,但是经常按完快捷键(F12)后,页面会进入 paused in debugger状态,需要点击右上角 ...

  4. Objective-C中#define的常见用法

    参考博客 http://blog.csdn.net/kindazrael/article/details/8108868 在C语言中,预处理代码是非常强大的工具,能让代码变得可读性和可维护性更强.预处 ...

  5. 前端工程筹建NodeJs+gulp+bower

    1.安装nodejs nodejs 官网下载安装文件 安装完成之后,在命令窗口执行,(显示nodejs版本) 和(显示npm版本)可以使用这两个命令查看是否安装成功: node -v npm -v 2 ...

  6. hdu1864

    use the cnt as the limit. #include <string.h> #include <stdio.h> ],sum; ]; double a,b,c; ...

  7. Android事件

    1.Java package com.fish.helloworld; import android.app.Activity; import android.graphics.Color; impo ...

  8. linux下多ISP的策略路由

    http://kerry.blog.51cto.com/172631/385578/ 修改/etc/iproute2/rt_tables 先在 /etc/iproute2/rt_tables 下建兩個 ...

  9. iptables用法

    iptables -t nat -A PREROUTING -s 10.10.10.0/24 -i eth1 -p tcp --dport 80 -j REDIRECT --to-ports 3128 ...

  10. CentOS 6.4安装AMH面板

    复制以下代码 然后执行 或者下载wget http://amysql.com/file/AMH/3.2/amh.sh; chmod 775 amh.sh; ./amh.sh 2>&1 | ...