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.

  

原文地址:https://www.cnblogs.com/aguncn/p/11713897.html

时间: 2024-08-27 04:58:16

golang中的接口的相关文章

golang中interface接口的深度解析

什么是interface,简单的说,interface是一组method的组合,下面这篇文章主要给大家深度解析了关于golang中的interface接口,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧. 一 接口介绍 如果说gorountine和channel是支撑起Go语言的并发模型的基石,让Go语言在如今集群化与多核化的时代成为一道亮丽的风景,那么接口是Go语言整个类型系列的基石,让Go语言在基础编程哲学的探索上达到前所

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

再golang中,我们要充分理解interface和struct这两种数据类型.为此,我们需要优先理解type的作用. type是golang语言中定义数据类型的唯一关键字.对于type中的匿名成员和指针成员,这里先不讲,重点讲解interface和struct这两种特殊的数据类型. interface和struct也是数据类型,特殊在于interface作为万能的接口类型,而struct作为常用的自定义数据类型的关键字.说到这里相比大家已经明白interface的侧重点在于接口的定义(方法),

Golang中使用log(一):Golang 标准库提供的Log

Golang的标准库提供了log的机制,但是该模块的功能较为简单(看似简单,其实他有他的设计思路).不过比手写fmt. Printxxx还是强很多的.至少在输出的位置做了线程安全的保护.其官方手册见Golang log (天朝的墙大家懂的).这里给出一个简单使用的例子: package main import ( "log" ) func main(){ log.Fatal("Come with fatal,exit with 1 \n") } 编译运行后,会看到程

Golang中的字节序列化操作

在写网络程序的时候,我们经常需要将结构体或者整数等数据类型序列化成二进制的buffer串.或者从一个buffer中解析出来一个结构体出来,最典型的就是在协议的header部分表征head length 或者body length在拼包和拆包的过程中,需要按照规定的整数类型进行解析,且涉及到大小端序的问题. 1.C中是怎么操作的 在C中我们最简单的方法是用memcpy来一个整形数或者结构体等其他类型复制到一块内存中,然后在强转回需要的类型.如:     // produce     int a =

Golang中使用log(二):Golang 标准库log的实现

前一篇文章我们看到了Golang标准库中log模块的使用,那么它是如何实现的呢?下面我从log.Logger开始逐步分析其实现. 其源码可以参考官方地址 1.Logger结构 首先来看下类型Logger的定义: type Logger struct { mu sync.Mutex // ensures atomic writes; protects the following fields prefix string // prefix to write at beginning of each

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

Golang中操作数据库已经有现成的库"database/sql"可以用,但是"database/sql"只提供了最基础的操作接口: 对数据库中一张表的增删改查等操作,必须手动编写sql string,这通常都是一个写死的字符串(Hard-Code), 并且需要手动维护sql中字段与Golang中的变量的映射关系,这扩展性很差,且非常容易出错. 通常情况下,我们期望Golang中存在一个Struct与DB中的一个Table建立一个映射关系(Mapper), 之后我们

在Golang中使用Redis

周五上班的主要任务是在公司老平台上用redis处理一个队列问题,顺便复习了一下redis操作的基础知识,回来后就想着在自己的博客demo里,用redis来优化一些使用场景,学习一下golang开发下redis的使用. Redis简单介绍 简介 关于Redis的讨论,其实在现在的后台开发中已经是个老生常谈的问题,基本上也是后端开发面试的基本考察点.其中 Redis的背景介绍和细节说明在这里就不赘述.不管怎么介绍,核心在于Redis是一个基于内存的key-value的多数据结构存储,并可以提供持久化

golang中Any类型使用及空接口中类型查询

golang中Any类型使用及类型查询1.Any类型GO语言中任何对象实例都满足空接口interface{},空接口可以接口任何值var v1 interface{} = 1 var v2 interface{} = "abc" var v3 interface{} = 2.345var v4 interface{} = make(map[..]...).... 2.1 关于空接口的类型查询方式一,使用ok package main import "fmt" //空

golang中创建logger时候踩过的坑

golang中创建logger时候踩过的坑 错误的代码 package main import ( "fmt" "io" "log" "os" ) var logger *log.Logger func init(){ fmt.Println("创建日记录日志文件") f,err:=os.OpenFile("./Log.log",os.O_WRONLY|os.O_CREATE|os.O_