Golang之interface(多态,类型断言)
多态用法
package main //一种事物的多种形态,都可以按照统一的接口进行操作
//多态
import (
"fmt"
"math/rand"
"sort"
) type Student struct {
Name string
Id string
Age int
sortType int
}
type Book struct {
Name string
Author string
} //切片默认传地址
type StudentArray []Student func (p StudentArray) Len() int {
return len(p)
} func (p StudentArray) Less(i, j int) bool {
return p[i].Name < p[j].Name
} func (p StudentArray) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
} func main() {
var stus StudentArray
for i := ; i < ; i++ {
stu := Student{
Name: fmt.Sprintf("stu%d", rand.Intn()),
Id: fmt.Sprintf("110%d", rand.Int()),
Age: rand.Intn(),
}
stus = append(stus, stu)
}
for _, v := range stus {
fmt.Println(v)
} fmt.Println("\n\n")
sort.Sort(stus)
for _, v := range stus {
fmt.Println(v)
}
}
接口嵌套
package main import "fmt"
//接口嵌套 一个接口可以嵌套在另外的接口
type Reader interface {
Read()
}
type Writer interface {
Write()
}
type ReadWriter interface {
Reader
Writer
}
type File struct {
} func (f *File) Read() {
fmt.Println("read data")
} func (f *File) Write() {
fmt.Print("write data")
}
func Test(rw ReadWriter) {
rw.Read()
rw.Write()
} func main() {
var f File
Test(&f)
}
类型断言
package main
import "fmt"
type Student struct {
Name string
Sex string
}
//类型断言
//一个判断传入参数类型的函数
func just(items ...interface{}) {
for index, v := range items {
switch v.(type) {
case bool:
fmt.Printf("%d params is bool,value is %v\n", index, v)
case int, int64, int32:
fmt.Printf("%d params is int,value is %v\n", index, v)
case float32, float64:
fmt.Printf("%d params is float,value is %v\n", index, v)
case string:
fmt.Printf("%d params is string,value is %v\n", index, v)
case Student:
fmt.Printf("%d params student,value is %v\n", index, v)
case *Student:
fmt.Printf("%d params *student,value is %v\n", index, v)
}
}
}
func main() {
var b Student = Student{
Name: "stu01",
Sex: "female",
}
just(, 8.2, "this is a test", b, &b)
}
Golang之interface(多态,类型断言)的更多相关文章
- Go interface{}、类型断言
在 golang 中 interface{} 可用于向函数传递任意类型的变量, 但在函数内部使用的话, 该变量的类型就是 interface{}, 也称为空接口类型 比如我们定义一个函数, 输出字符串 ...
- Golang-interface(四 反射)
github:https://github.com/ZhangzheBJUT/blog/blob/master/reflect.md 一 反射的规则 反射是程序执行时检查其所拥有的结构.尤其是类型的一 ...
- golang学习笔记:Interface类型断言详情
原文链接:https://www.2cto.com/kf/201712/703563.html 1. 用于判断变量类型 demo如下: switch t := var.(type){ case str ...
- golang 类型断言的学习
在php中有一个 serialize() 函数 可以把数组序列化成字符串进行存储和传输 如果想反序列化这种字符串,在php中只需要一个简单的unserialize() 函数就可以完成了.但是在gola ...
- golang类型断言
一.介绍 类型断言,由于接口是一般类型,不知道具体类型,如果要转成具体类型,就需要使用类型断言 例子: package main import "fmt" func main(){ ...
- [golang] go的typeswitch guard(类型区别)语法和type assertion(类型断言)语法
最近在实现golang,看到个go的特性语法: typeswitch guard. typeswitch guard语法如下: package main import "fmt" ...
- Golang | 既是接口又是类型,interface是什么神仙用法?
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是golang专题的第12篇文章,我们来继续聊聊interface的使用. 在上一篇文章当中我们介绍了面向对象的一些基本概念,以及gol ...
- [Go] golang类型断言
类型断言有点像向下转型,接口类型转到具体的实现实例类型上类型断言是一个使用在接口值上的操作.语法上它看起来像x.(T)被称为断言类型,这里x表示一个接口的类型和T表示一个类型 package main ...
- Golang的类型断言
类型断言即判断一个变量是不是某个类型的实例,这个经常用在判断接口的类型,基本的格式: y, ok := x.(type) 上面的语句用于判断变量x是不是type类型,有两种结果: x是type类型的变 ...
随机推荐
- java设计模式--七大原则
2016-05-14 20:45:38 设计模式到底是五大.六大还是七大,一直傻傻分不清楚,网上总是有说那些原则可以归为一个,我纠结了半天,突然发现自己是舍本逐末了,只要清楚这些原则的设计思想,其他的 ...
- php in_array的坑
今天做一个根据用户充值领礼包码的活动,遇到一个问题部分用户领礼包时会一直提示“系统错误,请稍后再试”,这是什么情况,一开始以为接口出错了,一番排查后发现了问题所在,是in_array坑了~~~ 情况大 ...
- Linux 解压zip need PK compat. v4.5 (can do v2.1)
p7z 当使用7zip压缩大于4G的文件后,在linux下解压时,出现下面的提示,无法解压: [root@localhost root]# unzip Wrestlemania20.zip Archi ...
- Linux vnc服务器操作(启动/停止/重起/自启动)
8.启动和停止VNC服务1)启动VNC服务命令[root@testdb ~]# /etc/init.d/vncserver startStarting VNC server: 1:rootNew 't ...
- 堆排序算法-python实现
#-*- coding: UTF-8 -*- import numpy as np def MakeHeap(a): for i in xrange(a.size / 2 - 1, -1, -1):# ...
- Apache Commons Codec 与消息摘要算法(hash算法)
首先我们要明白 Codec 是什么含义.它是 Coder + decoder = Codec,也就是编码器解码器.即是编码器,也是解码器. 官网地址:http://commons.apache.org ...
- make 写法练习
cc=g++ all:signal %:%.o $(cc) -o $< $@ %.cpp:%.o echo se $< $@ $* $^ g++ -c $< $@cl: rm -rf ...
- Bootstrap-Plugin:附加导航(Affix)插件
ylbtech-Bootstrap-Plugin:附加导航(Affix)插件 1.返回顶部 1. Bootstrap 附加导航(Affix)插件 附加导航(Affix)插件允许某个 <div&g ...
- Bootstrap-CL:分页
ylbtech-Bootstrap-CL:分页 1.返回顶部 1. Bootstrap 分页 本章将讲解 Bootstrap 支持的分页特性.分页(Pagination),是一种无序列表,Bootst ...
- 简易的RPC调用框架(大神写的)
RPC,即 Remote Procedure Call(远程过程调用),说得通俗一点就是:调用远程计算机上的服务,就像调用本地服务一样. RPC 可基于 HTTP 或 TCP 协议,Web Servi ...