多态用法

  1. package main
  2.  
  3. //一种事物的多种形态,都可以按照统一的接口进行操作
  4. //多态
  5. import (
  6. "fmt"
  7. "math/rand"
  8. "sort"
  9. )
  10.  
  11. type Student struct {
  12. Name string
  13. Id string
  14. Age int
  15. sortType int
  16. }
  17. type Book struct {
  18. Name string
  19. Author string
  20. }
  21.  
  22. //切片默认传地址
  23. type StudentArray []Student
  24.  
  25. func (p StudentArray) Len() int {
  26. return len(p)
  27. }
  28.  
  29. func (p StudentArray) Less(i, j int) bool {
  30. return p[i].Name < p[j].Name
  31. }
  32.  
  33. func (p StudentArray) Swap(i, j int) {
  34. p[i], p[j] = p[j], p[i]
  35. }
  36.  
  37. func main() {
  38. var stus StudentArray
  39. for i := ; i < ; i++ {
  40. stu := Student{
  41. Name: fmt.Sprintf("stu%d", rand.Intn()),
  42. Id: fmt.Sprintf("110%d", rand.Int()),
  43. Age: rand.Intn(),
  44. }
  45. stus = append(stus, stu)
  46. }
  47. for _, v := range stus {
  48. fmt.Println(v)
  49. }
  50.  
  51. fmt.Println("\n\n")
  52. sort.Sort(stus)
  53. for _, v := range stus {
  54. fmt.Println(v)
  55. }
  56. }

接口嵌套

  1. package main
  2.  
  3. import "fmt"
  4. //接口嵌套 一个接口可以嵌套在另外的接口
  5. type Reader interface {
  6. Read()
  7. }
  8. type Writer interface {
  9. Write()
  10. }
  11. type ReadWriter interface {
  12. Reader
  13. Writer
  14. }
  15. type File struct {
  16. }
  17.  
  18. func (f *File) Read() {
  19. fmt.Println("read data")
  20. }
  21.  
  22. func (f *File) Write() {
  23. fmt.Print("write data")
  24. }
  25. func Test(rw ReadWriter) {
  26. rw.Read()
  27. rw.Write()
  28. }
  29.  
  30. func main() {
  31. var f File
  32. Test(&f)
  33. }

类型断言

  1. package main
  2.  
  3. import "fmt"
  4.  
  5. type Student struct {
  6. Name string
  7. Sex string
  8. }
  9. //类型断言
  10. //一个判断传入参数类型的函数
  11. func just(items ...interface{}) {
  12. for index, v := range items {
  13. switch v.(type) {
  14. case bool:
  15. fmt.Printf("%d params is bool,value is %v\n", index, v)
  16. case int, int64, int32:
  17. fmt.Printf("%d params is int,value is %v\n", index, v)
  18. case float32, float64:
  19. fmt.Printf("%d params is float,value is %v\n", index, v)
  20. case string:
  21. fmt.Printf("%d params is string,value is %v\n", index, v)
  22. case Student:
  23. fmt.Printf("%d params student,value is %v\n", index, v)
  24. case *Student:
  25. fmt.Printf("%d params *student,value is %v\n", index, v)
  26.  
  27. }
  28. }
  29. }
  30. func main() {
  31. var b Student = Student{
  32. Name: "stu01",
  33. Sex: "female",
  34. }
  35. just(, 8.2, "this is a test", b, &b)
  36.  
  37. }

Golang之interface(多态,类型断言)的更多相关文章

  1. Go interface{}、类型断言

    在 golang 中 interface{} 可用于向函数传递任意类型的变量, 但在函数内部使用的话, 该变量的类型就是 interface{}, 也称为空接口类型 比如我们定义一个函数, 输出字符串 ...

  2. Golang-interface(四 反射)

    github:https://github.com/ZhangzheBJUT/blog/blob/master/reflect.md 一 反射的规则 反射是程序执行时检查其所拥有的结构.尤其是类型的一 ...

  3. golang学习笔记:Interface类型断言详情

    原文链接:https://www.2cto.com/kf/201712/703563.html 1. 用于判断变量类型 demo如下: switch t := var.(type){ case str ...

  4. golang 类型断言的学习

    在php中有一个 serialize() 函数 可以把数组序列化成字符串进行存储和传输 如果想反序列化这种字符串,在php中只需要一个简单的unserialize() 函数就可以完成了.但是在gola ...

  5. golang类型断言

    一.介绍 类型断言,由于接口是一般类型,不知道具体类型,如果要转成具体类型,就需要使用类型断言 例子: package main import "fmt" func main(){ ...

  6. [golang] go的typeswitch guard(类型区别)语法和type assertion(类型断言)语法

    最近在实现golang,看到个go的特性语法: typeswitch guard. typeswitch guard语法如下: package main import "fmt" ...

  7. Golang | 既是接口又是类型,interface是什么神仙用法?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是golang专题的第12篇文章,我们来继续聊聊interface的使用. 在上一篇文章当中我们介绍了面向对象的一些基本概念,以及gol ...

  8. [Go] golang类型断言

    类型断言有点像向下转型,接口类型转到具体的实现实例类型上类型断言是一个使用在接口值上的操作.语法上它看起来像x.(T)被称为断言类型,这里x表示一个接口的类型和T表示一个类型 package main ...

  9. Golang的类型断言

    类型断言即判断一个变量是不是某个类型的实例,这个经常用在判断接口的类型,基本的格式: y, ok := x.(type) 上面的语句用于判断变量x是不是type类型,有两种结果: x是type类型的变 ...

随机推荐

  1. BZOJ4372: 烁烁的游戏【动态点分治】

    Description 背景:烁烁很喜欢爬树,这吓坏了树上的皮皮鼠. 题意: 给定一颗n个节点的树,边权均为1,初始树上没有皮皮鼠. 烁烁他每次会跳到一个节点u,把周围与他距离不超过d的节点各吸引出w ...

  2. Oracle单表去重复(二)

    Oracle单表去重 去重有两层含义,一:是记录完全一样.二:是符合一定条件的认为是重复. 根据表的数量,去重可划分为:单表去重和多表关联去重.   对于去重,一般最容易想到的是用distinct,而 ...

  3. sublime text 3 配置优化

    1.在用Sublime里编写代码时,需要把TAB键转换成四个空格. 菜单栏里点击:首选项-> 设置-用户(Preferences-> Setting-User)在弹出来的文本里,添加如下两 ...

  4. rsync 通过密码文件实现远程同步

    https://my.oschina.net/yyping/blog/91964 1.源文件服务器:192.168.10.203 2.备份服务器:192.168.10.88 配置备份服务器(192.1 ...

  5. [C++ Primer] 第7章: 类

    定义抽象数据类型 定义在类内部的函数是隐式的inline函数. const成员函数 又叫做常量成员函数, 成员函数参数列表之后紧跟const关键字, const修饰的是类this指针. 默认情况下th ...

  6. Java-Runoob-高级教程-实例-字符串:06. Java 实例 - 字符串查找

    ylbtech-Java-Runoob-高级教程-实例-字符串:06. Java 实例 - 字符串查找 1.返回顶部 1. Java 实例 - 字符串搜索  Java 实例 以下实例使用了 Strin ...

  7. expect学习笔记及用法

    expect学习笔记及实例详解 expect的基本用法 expect用法

  8. 使用PHP自带zlib函数 几行代码实现PHP文件打包下载zip

    <?php //获取文件列表 function list_dir($dir){ $result = array(); if (is_dir($dir)){ $file_dir = scandir ...

  9. JDK、Spring、Quartz等几种不同定时器的用法,以及cronExpression表达式定义

    referenc:https://blog.csdn.net/clementad/article/details/42042111 下面介绍几种常用的定时器及其实现方法: 第一种:Timer和Time ...

  10. predict.glm -> which class does it predict?

    Jul 10, 2009; 10:46pm predict.glm -> which class does it predict? 2 posts Hi, I have a question a ...