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 ...
随机推荐
- SQL Server存储过程备份数据库的脚本-干货
拿AAAAAAAA数据库为例子,除了汉字标注的替换真实的数据,其他都不动 CREATE proc [dbo].[p_log_backupAAAAAAAA] @dbname sysn ...
- Notification 弹出一个通知在桌面右下角
if (!("Notification" in window)) { //alert("This browser does not support desktop not ...
- CSS绘制三角形和箭头,不用再用图片了
前言 还在用图片制作箭头,三角形,那就太lou了.css可以轻松搞定这一切,而且颜色大小想怎么变就怎么变,还不用担心失真等问题. 先来看看这段代码: /**css*/.d1{ width: 0; he ...
- django之查询操作及开启事务
目录 聚合查询 aggregate 聚合函数 分组查询 annotate F与Q查询 F查询 Q查询 ORM操作事务 django中开启事务 聚合查询 aggregate 操作外键字段管理数据的时候, ...
- What is Market Intelligence and how is it Used?
https://blog.globalwebindex.com/marketing/market-intelligence/ Market intelligence is the gathering ...
- 这样修改有哪些优缺点 wcf service via attribute setting vs config
客户要恢复数据,结果就是block在某个阶段,在server端log一圈下来,发现原来是client端出了问题,就是这个log: ERROR - Identity check failed for o ...
- 如何创建Azure Face API和计算机视觉Computer Vision API
在人工智能技术飞速发展的当前,利用技术手段实现人脸识别.图片识别已经不是什么难事.目前,百度.微软等云计算厂商均推出了人脸识别和计算机视觉的API,其优势在于不需要搭建本地环境,只需要通过网络交互,就 ...
- python接口自动化5-session关联
前言 我们不难发现浏览器中存在着cookie缓存等,但我们在python中如果像浏览器这样的缓存,我们就很难的需要关联cookie或会话了. 但python的requests库,就封装了Session ...
- 10-scrapy框架介绍
Scrapy 入门教程 Scrapy 是用 Python 实现的一个为了爬取网站数据.提取结构性数据而编写的应用框架. Scrapy 常应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. 通 ...
- Javascript模块化开发1——package.json详解
一.环境安装 Node.js 安装包及源码下载地址为:https://nodejs.org/en/download/. 在该页面你可以根据不同平台系统选择你需要的 Node.js 安装包. Node. ...