golang中的接口
CSDN找的一个网页,照着抄练一次。
差不多的使用场景都在了。
package main import ( "fmt" ) type People interface { ReturnName() string } type Role interface { People ReturnRole() string } type Student struct { Name string } type Teacher struct { Name string } func (s Student) ReturnName() string { return s.Name } func (t *Teacher) ReturnName() string { return t.Name } func (s Student) ReturnRole() string { return "student" } func CheckPeople(test interface{}) { if _, ok := test.(People); ok { fmt.Println("Student implements People") } } func main() { cbs := Student{Name: "This is a student."} sss := Teacher{Name: "This is a teacher."} CheckPeople(cbs) var a People var b Role a = cbs name := a.ReturnName() fmt.Println(name) a = &sss name = a.ReturnName() fmt.Println(name) b = cbs role := b.ReturnRole() fmt.Println(role) Params := make([]interface{}, 3) Params[0] = 88 Params[1] = "This is a string" Params[2] = Student{Name: "cbs"} for index, v := range Params { if _, ok := v.(int); ok { fmt.Printf("Params[%d] is int type\n", index) } else if _, ok := v.(string); ok { fmt.Printf("Params[%d] is string type\n", index) } else if _, ok := v.(Student); ok { fmt.Printf("Params[%d] is custom Student type\n", index) } else { fmt.Printf("list[%d] unknown type.\n", index) } } for index, v := range Params { switch value := v.(type) { case int: fmt.Printf("Params[%d] is int type: %d\n", index, value) case string: fmt.Printf("Params[%d] is string type: %s\n", index, value) case Student: fmt.Printf("Params[%d] is custom Student type: %s\n", index, value) default: fmt.Printf("list[%d] unknown type.\n", index) } } }
输出:
D:/go-project/src/InterfaceGo/InterfaceGo.exe [D:/go-project/src/InterfaceGo] Student implements People This is a student. This is a teacher. student Params[0] is int type Params[1] is string type Params[2] is custom Student type Params[0] is int type: 88 Params[1] is string type: This is a string Params[2] is custom Student type: {cbs} 成功: 进程退出代码 0.
golang中的接口的更多相关文章
- golang中的接口实现(一)
golang中的接口实现 // 定义一个接口 type People interface { getAge() int // 定义抽象方法1 getName() string // 定义抽象方法2 } ...
- golang中的接口值
package main import ( "bytes" "fmt" "io" ) // 此处的w参数默认是一个空接口,当传递进来buf参 ...
- golang中的接口实现(二)
指针类型 vs 值类型实现接口 package main import ( "fmt" ) // 定义接口 type Describer interface { Describe( ...
- 七、golang中接口、反射
一.接口定义 1.定义 interface类型可以定义一组方法,但是这些不需要实现,并且interface不能包含任何变量 package main import ( "fmt" ...
- golang中接口interface和struct结构类的分析
再golang中,我们要充分理解interface和struct这两种数据类型.为此,我们需要优先理解type的作用. type是golang语言中定义数据类型的唯一关键字.对于type中的匿名成员和 ...
- 六、golang中的结构体和方法、接口
结构体: 1.用来自定义复杂数据结构 2.struct里面可以包含多个字段(属性) 3.struct类型可以定义方法,注意和函数的区分 4.strucr类型是值类型 5.struct类型可以嵌套 6. ...
- Golang 中的 面向对象: 方法, 类, 方法继承, 接口, 多态的简单描述与实现
前言: Golang 相似与C语言, 基础语法与C基本一致,除了广受争议的 左花括号 必须与代码同行的问题, 别的基本差不多; 学会了C, 基本上万变不离其宗, 现在的高级语言身上都能看到C的影子; ...
- Golang中Struct与DB中表字段通过反射自动映射 - sqlmapper
Golang中操作数据库已经有现成的库"database/sql"可以用,但是"database/sql"只提供了最基础的操作接口: 对数据库中一张表的增删改查 ...
- golang中发送http请求的几种常见情况
整理一下golang中各种http的发送方式 方式一 使用http.Newrequest 先生成http.client -> 再生成 http.request -> 之后提交请求:clie ...
随机推荐
- MySQL之架构简单分析
上图为MySQL的简易架构图,给您有一个大概的概念,下面我将为您进行进一步的分析. 连接器: 当连接MySQL数据库时,等待的将是MySQL服务端的连接器:连接器的职责是和客户端建立连接.获取权限.维 ...
- hidraw设备简要分析
关键词:hid.hidraw.usbhid.hidp等等. 下面首先介绍hidraw设备主要用途,然后简要分析hidraw设备驱动(但是不涉及到相关USB/Bluwtooth驱动),最后分析用户空间接 ...
- day89_11_11Flask启动,配置,路由,fbv和cbv
一.flask的形成. flask是一个基于python并且以来jinja2模板和werkzeug wsgi服务器的一个微型框架. 安装了flask模块就代表安装了wekzeug,所以先安装flask ...
- 数位DP入门详解+题目推荐
\(update:2019-9-6\) 博客里某些东西没有解释清楚,完善了对应的解释 在开始之前,我们先来看一道题--题目链接 题目要求,相邻两位的差大于等于2,那么我们先来构造一个试一试. 比如说\ ...
- java之位运算符
整型转二进制:Integer.toBInaryString(6) <<:左移,3<<2 = 3*2*2 = 12 >>:右移,3>>1 = 3/2 = ...
- Spring中注解方式实现IOC和AOP
1.IOC注解 1.1 IOC和DI的注解 IOC: @Component:实现Bean组件的定义 @Repository:用于标注DAO类,功能与@Component作用相当 @Service:用 ...
- HTML+css基础 标签
图片标签:<img src="./imgs/1.jpg" alt=“一种对图片解释说明的” /> HTML 超文本标记语言 英文名称: hyper text ...
- 【LOJ#6682】梦中的数论(min_25筛)
[LOJ#6682]梦中的数论(min_25筛) 题面 LOJ 题解 注意题意是\(j|i\)并且\((j+k)|i\), 不难发现\(j\)和\((j+k)\)可以任意取\(i\)的任意因数,且\( ...
- bootstrap table使用colResizable后表格不能自适应
窗口缩小放大后,b表格不能自适应,table加了宽度没效果,求教
- SpringBoot 教程之属性加载详解
免费Java高级资料需要自己领取,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G. ...