接口定义

Interface类型可以定义一组方法,但是这些不需要实现。并且interface不能 包含任何变量。

  1. type Interface interface {
  2.   test1(a, b int) bool
  3.   test2()
  4. }

interface类型默认是一个指针。

空接口(a interface{})可以被任何类型所实现,如动物可以被猫、狗、人等实现,反之人不一定能实现动物

  1. func main() {
  2.   var a interface{}
  3.  
  4.   var b int
  5.   a = b
  6.  
  7.   fmt.Printf("%T\n", a)
  8.  
  9.   var s string
  10.   a = s
  11.  
  12.   fmt.Printf("%T", a)
  13. }

接口只是一种规范,并没有实现,不能直接调用

  1. type testInterface int
  2.  
  3. func (t testInterface) test1(a, b int) bool {
  4. return a < b
  5. }
  6.  
  7. func (t testInterface) test2() {
  8. fmt.Println("test2")
  9. }
  10.  
  11. func main() {
  12. var a Interface
  13. a.test2()
  14. }

接口实现

Golang中的接口,不需要显示的实现。只要一个变量,含有接口类型中的所有方法,那么这个变量就实现这个接口。因此,golang中没有implement 类似的关键字。

  1. type testInterface int
  2.  
  3. func (t testInterface) test1(a, b int) bool {
  4.   return a < b
  5. }
  6.  
  7. func (t testInterface) test2() {
  8.   fmt.Println("test2")
  9. }
  10.  
  11. func main() {
  12.   var a testInterface
  13.   fmt.Printf("%T\n", a.test1(1, 2))
  14.   a.test2()
  15. }

接口实现时需要实现规范内的所有方法,否则会报错

  1. type Interface interface {
  2. test1(a, b int) bool
  3. test2()
  4. }
  5.  
  6. type testInterface int
  7.  
  8. func (t testInterface) test1(a, b int) bool {
  9. return a < b
  10. }
  11.  
  12. func main() {
  13. var test testInterface
  14. var i Interface
  15.  
  16. i = test
  17.  
  18. fmt.Println(test)
  19.  
  20. }

cannot use test (type testInterface) as type Interface in assignment:
testInterface does not implement Interface (missing test2 method)

如果一个变量含有了多个interface类型的方法,那么这个变量就实现了多个 接口。

  1. type human interface { //人类
  2. get_country() //国家
  3. }
  4.  
  5. type lang interface {
  6. speek() //语言
  7. }
  8.  
  9. type chinesePeople struct { //中国人
  10. country string
  11. }
  12.  
  13. func (c chinesePeople) get_country() {
  14. fmt.Println(c.country)
  15. }
  16.  
  17. type japanese struct { //日本人
  18. country string
  19. }
  20.  
  21. func (c japanese) get_country() {
  22. fmt.Println(c.country)
  23. }
  24.  
  25. func (c japanese) speek() { //说的语言
  26. fmt.Println("ハロー")
  27. }
  28.  
  29. func main() {
  30.  
  31. var c = chinesePeople{
  32. country: "china",
  33. }
  34.  
  35. var j = japanese{
  36. country: "japan",
  37. }
  38. var h human
  39. var langage lang
  40.  
  41. h = c
  42.  
  43. h.get_country()
  44.  
  45. langage = j
  46. h = j
  47. h.get_country()
  48. langage.speek()
  49. }

japanese结构体实现了两个接口

chinese结构体实现了country接口如果使用lang赋值会提示未实现该接口

cannot use c (type chinesePeople) as type lang in assignment:
chinesePeople does not implement lang (missing speek method)

接口嵌套

一个接口可以嵌套在另外的接口

  1. package main
  2.  
  3. type human interface {
  4. run()
  5. eyes()
  6. }
  7.  
  8. type info interface {
  9. country()
  10. sex()
  11. }
  12.  
  13. type chinesePeople interface {
  14. human
  15. info
  16. }
  17.  
  18. func main() {
  19.  
  20. }

类型断言

接口是一般类型,不知道具体类型,如果要转成具体类型

语法

  1. 目标类型 := x.(t)
  2. 目标类型,布尔值 := x.(t)

两者区别为,安全类型与不安全类断言,非安全类型如果判定失败,这个操作会抛出panic

如:

  1. var i interface{}
  2. i = 10
  3. j := i.(string)
  4. fmt.Printf("%T\n", j)

断言的操作对象x是一个nil接口值,那么不论被断言的类型T是什么这个类型断言都会失败。

  1. var i interface{}
  2. i = nil
  3. j := i.(nil)

安全类型的断言

  1. t, ok := i.(int)
  2. if ok == false {
  3.   fmt.Printf("失败,断言的类型为%T,而i的类型为%T", t, i)
  4.   os.Exit(1)
  5. }
  6.  
  7. fmt.Printf("成功%T", t)

  

golang interface的更多相关文章

  1. Golang Interface 解析

    转自 https://zhuanlan.zhihu.com/p/27652856 先看一段代码: 123456789101112 func (x interface{}) { if x == nil ...

  2. golang:interface{}类型测试

    在golang中空的interface即interface{}可以看作任意类型, 即C中的void *. 对interface{}进行类型测试有2种语法: 1. Comma-ok断言: value, ...

  3. 工作随笔——Golang interface 转换成其他类型

    新的公司,新的氛围.一年了,打算写点什么.so,那就写google的golang语言吧. 最最最基础的语法结构见go语言菜鸟教程 接下来写点菜鸟教程没有的. go语言的设计者认为:go语言必须让程序员 ...

  4. golang interface判断为空nil

    要判断interface 空的问题,首先看下其底层实现. interface 底层结构 根据 interface 是否包含有 method,底层实现上用两种 struct 来表示:iface 和 ef ...

  5. 浅析 golang interface 实现原理

    interface 在 golang 中是一个非常重要的特性.它相对于其它语言有很多优势: duck typing.大多数的静态语言需要显示的声明类型的继承关系.而 golang 通过 interfa ...

  6. golang interface 多态

    原文链接:http://www.52bd.net/code/210.html demo: package main import ( "fmt" ) //通知行为的接口 type ...

  7. golang interface 类型学习

    接口类型变量的内存结构 动态类型 动态值 对于动态类型指的是当其他非接口类型变量赋值给接口类型变量时,接口类型变量中的动态类型就是当前非接口类型 对于动态值指的就是当其他非接口类型变量赋值给接口类型变 ...

  8. golang interface类型转string等其他类型

    inter 是interface类型,转化为string类型是: str := inter .(string) 转为其他类型也类似

  9. golang interface 转 string,int,float64

    func interface2String(inter interface{}) { switch inter.(type) { case string: fmt.Println("stri ...

随机推荐

  1. django.db.utils.DataError: (1406, "Data too long for column 'gender' at row 1")

    报错现象 在使用 django 创建 超级用户的时候提示报错 Password (again): ytyt521521 Traceback (most recent call last): File ...

  2. Hdoj 2199.Can you solve this equation? 题解

    Problem Description Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solutio ...

  3. Android里透明的ListView

    发现了一个list滚动时,某item背景透明的问题.网上搜索一下,发现有很多人在问list背景黑色的问题,交流中给出的解决方案基本上很统一. 先是解释问题产生的原因是Android对list的滚动做了 ...

  4. [FJOI2016]神秘数(脑洞+可持久化)

    题目描述 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13}, 1 = 1 2 = 1+1 3 = 1+1+1 4 = 4 5 = 4+1 6 = ...

  5. Centos7下源码编译安装python3.6

    测试环境: 操作步骤: 1. 下载Python源码包(python3.6.0) 官网下载地址:https://www.python.org/downloads/ 搜狐下载地址:http://mirro ...

  6. hdu 1978 How many ways(记忆化搜索)

    这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下:1.机器人一开始在棋盘的起始点并有起始点所标有的能量.2.机器人只能向右或者向下走,并 ...

  7. C语言中类型转换#大写字母转小写字母和小写字母转大写字母案例。

    先让我们来看个很重要的东西,还是ASCII码. 十进制:A-Z:65-90十进制:a-z:97-122 了解这个很重要. 现在我们先举例子大写字母转小写字母案例: #include <stdio ...

  8. spring boot 连接mysql mongodb with jpa

    https://github.com/bigben0123/gs-accessing-data-mysql-mongo-jpa

  9. User Agent 用户代理

    User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本.CPU 类型.浏览器及版本.浏览器渲染引擎.浏览器语言.浏览器插件等. User A ...

  10. tfs 2013 利用 web deploy 完成asp.net站点自动发布

    课题起因: 目前我们团队使用visual studio 2013开发asp.net项目, 使用tfs2013 做源码管理, 每天早上手动发布项目文件包,复制到测试服务器的站点文件夹下覆盖老文件,用此方 ...