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中的接口的更多相关文章

  1. golang中的接口实现(一)

    golang中的接口实现 // 定义一个接口 type People interface { getAge() int // 定义抽象方法1 getName() string // 定义抽象方法2 } ...

  2. golang中的接口值

    package main import ( "bytes" "fmt" "io" ) // 此处的w参数默认是一个空接口,当传递进来buf参 ...

  3. golang中的接口实现(二)

    指针类型 vs 值类型实现接口 package main import ( "fmt" ) // 定义接口 type Describer interface { Describe( ...

  4. 七、golang中接口、反射

    一.接口定义 1.定义 interface类型可以定义一组方法,但是这些不需要实现,并且interface不能包含任何变量 package main import ( "fmt" ...

  5. golang中接口interface和struct结构类的分析

    再golang中,我们要充分理解interface和struct这两种数据类型.为此,我们需要优先理解type的作用. type是golang语言中定义数据类型的唯一关键字.对于type中的匿名成员和 ...

  6. 六、golang中的结构体和方法、接口

    结构体: 1.用来自定义复杂数据结构 2.struct里面可以包含多个字段(属性) 3.struct类型可以定义方法,注意和函数的区分 4.strucr类型是值类型 5.struct类型可以嵌套 6. ...

  7. Golang 中的 面向对象: 方法, 类, 方法继承, 接口, 多态的简单描述与实现

    前言: Golang 相似与C语言, 基础语法与C基本一致,除了广受争议的 左花括号 必须与代码同行的问题, 别的基本差不多; 学会了C, 基本上万变不离其宗, 现在的高级语言身上都能看到C的影子; ...

  8. Golang中Struct与DB中表字段通过反射自动映射 - sqlmapper

    Golang中操作数据库已经有现成的库"database/sql"可以用,但是"database/sql"只提供了最基础的操作接口: 对数据库中一张表的增删改查 ...

  9. golang中发送http请求的几种常见情况

    整理一下golang中各种http的发送方式 方式一 使用http.Newrequest 先生成http.client -> 再生成 http.request -> 之后提交请求:clie ...

随机推荐

  1. Linux数据库的创建 导入导出 以及一些基本指令

    首先linux 下查看mysql相关目录 查看 mysql 的安装路径 执行查询 SQL mysql>show variables like '%dir%'; datadir 就是数据路径 确定 ...

  2. Linux使用BIND提供域名解析服务

    DNS(Domain Name System,域名系统)用于管理和解析域名与IP地址对应关系的技术. 主服务器:在特定区域内具有唯一性,负责维护该区域内的域名与IP地址之间的对应关系. 从服务器:从主 ...

  3. LRU的实现(使用list)

    首先是LRU的定义,LRU表示最近最少使用,如果数据最近被访问过,那么将来被访问的几率也更高. 所以逻辑应该是每次都要将新被访问的页放到列表头部,如果超过了list长度限制,就将列表尾部的元素踢出去. ...

  4. Mac打开Finder快捷键

    摘要:目前网络中较常见的打开Finder的方法有两种,要么是先进入桌面状态,再使用快捷键command + shift + c:要么是通过下载软件来设置打开Finder的快捷键.都过于繁琐,其实有很简 ...

  5. javax.imageio.IIOException: Can't read input file!完美解决

    今天遇到一个问题,上图 这段代码主要是给图片添加水印 后来百度发现可能是图片的路径出了问题,因为是动态获取的图片地址,然后我把地址打印出来了 之后通过终端查看,果然没有 之后我在classes目录找到 ...

  6. C语言中,关于排序的问题(输入n个数,输出最大的那个)

    int n,max=0,t; scanf("%d",&n); int a[n],i,k;  //这个a[n]必须要在输入n的值之后才能定义,不然定义不成. for(i=1; ...

  7. eclipse中的clean操作

    在eclipse中写JavaWeb项目时,有时候会出现代码修改了,但是执行的效果还是修改之前的,这时候clean一下就会解决问题 1.clean操作 Project---->clean---&g ...

  8. Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest dp

    E. The Contest A team of three programmers is going to play a contest. The contest consists of

  9. Codeforces Round #594 (Div. 2) B. Grow The Tree 水题

    B. Grow The Tree Gardener Alexey teaches competitive programming to high school students. To congrat ...

  10. Golang 入门 : channel(通道)

    笔者在<Golang 入门 : 竞争条件>一文中介绍了 Golang 并发编程中需要面对的竞争条件.本文我们就介绍如何使用 Golang 提供的 channel(通道) 消除竞争条件. C ...