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 ...
随机推荐
- 解决element-ui的表格设置固定栏后,边框线消失的bug
如上图所示,边框线消失了,解决方法如下 添加css代码,如果是修改全局,则到全局样式文件添加 .el-table__row{ td:not(.is-hidden):last-child{ right: ...
- MyBtis之关于#{}和${}
1.${}的用法的mapper文件配置: </select> <select id="selById2" resultType="com.sus ...
- fastadmin中js是如何调用的
想要了解fastadmin中的js是怎么调用的,就应该先了解RequireJs. RequireJs是模块化工具,每一个我们自己的js文件或者库都可以看成是一个模块,按需引入.写法如下: <sc ...
- 010.MongoDB备份恢复
一 MongoDB备份 1.1 备份概述 mongodb数据备份和还原主要分为二种,一种是针对于库的mongodump和mongorestore,一种是针对库中表的mongoexport和mongoi ...
- LG5196 「USACO2019JAN」Cow Poetry 背包+乘法原理
\(\mathrm{Cow Poetry}\) 问题描述 LG5196 题解 因为每句诗的长度一定是\(k\),所以自然而然想到背包. 设\(opt[i][j]\)代表到第\(i\)位时,结尾为\(j ...
- P3525 INS-Inspection
这道题的题面有点问题,如果按照题面做,应该是A不了的,下面引用一下评论里@REM_001的翻译 一棵n个节点的树,行动中心S从1->N.从S出发前往任意一个未标记到的点(沿树上两点的唯一路径走) ...
- 防止ssh暴力破解的小工具denyhosts
DenyHosts 简介 DenyHosts 是 Python 语言写的一个程序软件,运行于 Linux 上预防 SSH 暴力破解的,它会分析 sshd 的日志文件(/var/log/secure), ...
- 1+x 证书 Web 前端开发中级理论考试(试卷 6 )
1+x 证书 Web 前端开发中级理论考试(试卷 6 ) 官方QQ群 1+x 证书 web 前端开发初级对应课程分析 http://blog.zh66.club/index.php/archives/ ...
- JDBC释放数据库连接
try(){}写法会自动关闭连接 String sql = "select password from user where name = ?"; try(Connection c ...
- 英语阅读——Love and logic:The story of a fallacy
这篇文章是<新视野大学英语>第四册的第一单元的文章,读着挺有趣,便拿过来分享一下. 1 I had my first date with Polly after I made the tr ...