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 ...
随机推荐
- 【分享】nginx负载均衡全套视频教程
1.课件 百度网盘链接:https://pan.baidu.com/s/1On2oONVZmPwI9yIDekgRiA 提取码:c4fw 2.教程列表 3.教程下载 3.1.直接在线学习 ...
- read write方式打开PHYSICAL STANDBY,闪回和还原测试
以下大部分都在STANDBY执行,主库执行(两次)的会提示 [STANDBY read write方式打开测试]检查standby状态SQL> SELECT NAME,DATABASE_ROLE ...
- Scrapy安装和简单使用
模块安装 Windows 安装scrapy 需要安装依赖环境twisted,twisted又需要安装C++的依赖环境 pip install scrapy 时 如果出现twisted错误 在http ...
- Python入门基础学习(模块,包)
Python基础学习笔记(五) 模块的概念:模块是python程序架构的一个核心概念 每个以拓展名py结尾的python源代码文件都是一个模块 模块名同样也是一个标识符,需要符合标识符的命名规则 在模 ...
- 并发相关基础知识 - MESI - JMM
一.CPU多级缓存 CPU的频率太快了,快到主存跟不上,这样在处理器时钟周期内,CPU常常需要等待主存,浪费资源,所以cache的出现,是为了缓解CPU和内存之间速度的不匹配问题.CPU多级缓存配置( ...
- Ubuntu 18.04 安装 pip3
Ubuntu 18.04 默认安装了 python2.x 和 python3.x:默认情况下 python 指的是 python2.x,如果要使用 python3.x 需要使用 python3,如: ...
- CF1269A Equation
题目链接 题意 要找两个合数,使他们两个的差为\(n\),\(n\)为题目给出的数 思路 我们可以枚举减数\(now\),判断一下是不是质数,如果是质数就让\(now++\),然后用一个数\(tot\ ...
- Python程序中的进程操作-进程同步(multiprocess.Lock)
目录 一.多进程抢占输出资源 二.使用锁维护执行顺序 三.多进程同时抢购余票 四.使用锁来保证数据安全 通过刚刚的学习,我们千方百计实现了程序的异步,让多个任务可以同时在几个进程中并发处理,他们之间的 ...
- IT兄弟连 HTML5教程 HTML5的学习线路图 第一阶段学习网页制作
学习HTML5技术可并不是简单学会几个新增的标签而已,HTML5现在可以说是前端所有技术的代名词.需要学习的语言和工具不仅多,对于刚接触他们的新人会感觉很乱.另外,前端开发也会细分很多个开发岗位,不同 ...
- selectpage选择订单的时候,订单数量和金额会动态改变
1. 2. HTML部分: JS: PHP获取数据并return json