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 ...
随机推荐
- [Go] 使用protobuf进行序列化和反序列化
先定义消息类型 orders.proto syntax = "proto2"; package message; message Orders { required int32 o ...
- 安装pymssql
直接安装失败 https://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql 去下载对应的 pymssql whl版本 之后 pip install whe ...
- AcWing 799. 最长连续不重复子序列
网址 https://www.acwing.com/solution/AcWing/content/2069/ 题目描述给定一个长度为n的整数序列,请找出最长的不包含重复数字的连续子序列,输出它的长 ...
- [C0] 引言(Introduction)
引言(Introduction) 欢迎(Welcome) 机器学习是目前信息技术中最激动人心的方向之一.在这门课中,你将学习到这门技术的前沿,并可以自己实现学习机器学习的算法. 你或许每天都在不知不觉 ...
- Java成员变量和局部变量区别
成员变量和局部变量区别 变量根据定义位置的不同,我们给变量起了不同的名字.如下图所示: 区别 在类中的位置不同 (重点) 成员变量:类中,方法外 局部变量:方法中或者方法声明上(形式参数) 作用范围 ...
- 剑指Offer-35.两个链表的第一个公共结点(C++/Java)
题目: 输入两个链表,找出它们的第一个公共结点. 分析: 先统计两个链表的长度,计算他们的差值,然后将两个链表对齐,再去寻找公共节点即可. 程序: C++ class Solution { publi ...
- R语言-laohuji
项目三-tiger机 说明:每玩一次老ji游戏需要花费一元钱.钻石符号(DD)可以百搭,并且能够将最终的金额加倍. 任务分解: 任务分解的步骤: 将复杂的任务分解为一些简单的子任务: 使用实例: 用通 ...
- MacOS命令行打包+签名+公证+生成dmg文件
关于dmg文件是什么,和为什么要进行公证? 简单说下,dmg文件就是一个可直接在mac上安装的安装包,我自己的理解是就像windows上的exe安装包一样: 公证是将app传到苹果商店去做认证,如果不 ...
- python进阶之内存模型
每一个编程语言的背后都有自己独特的内存模型支持,比如最经典的C语言,一个int类型占8字节.那么在python中不区分数据类型,定义一个变量其在内存在占用多少字节呢?python中数据的运算其内存是如 ...
- OpenFOAM——过渡管中的湍流
本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL016:Turbulent Flow in a Transition Duct 一 ...