接口定义

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

type Interface interface {
  test1(a, b int) bool
  test2()
}

interface类型默认是一个指针。

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

func main() {
  var a interface{}   var b int
  a = b   fmt.Printf("%T\n", a)   var s string
  a = s   fmt.Printf("%T", a)
}

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

type testInterface int

func (t testInterface) test1(a, b int) bool {
return a < b
} func (t testInterface) test2() {
fmt.Println("test2")
} func main() {
var a Interface
a.test2()
}

接口实现

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

type testInterface int

func (t testInterface) test1(a, b int) bool {
  return a < b
} func (t testInterface) test2() {
  fmt.Println("test2")
} func main() {
  var a testInterface
  fmt.Printf("%T\n", a.test1(1, 2))
  a.test2()
}

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

type Interface interface {
test1(a, b int) bool
test2()
} type testInterface int func (t testInterface) test1(a, b int) bool {
return a < b
} func main() {
var test testInterface
var i Interface i = test fmt.Println(test) }

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

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

type human interface { //人类
get_country() //国家
} type lang interface {
speek() //语言
} type chinesePeople struct { //中国人
country string
} func (c chinesePeople) get_country() {
fmt.Println(c.country)
} type japanese struct { //日本人
country string
} func (c japanese) get_country() {
fmt.Println(c.country)
} func (c japanese) speek() { //说的语言
fmt.Println("ハロー")
} func main() { var c = chinesePeople{
country: "china",
} var j = japanese{
country: "japan",
}
var h human
var langage lang h = c h.get_country() langage = j
h = j
h.get_country()
langage.speek()
}

japanese结构体实现了两个接口

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

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

接口嵌套

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

package main

type human interface {
run()
eyes()
} type info interface {
country()
sex()
} type chinesePeople interface {
human
info
} func main() { }

类型断言

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

语法

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

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

如:

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

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

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

安全类型的断言

t, ok := i.(int)
if ok == false {
  fmt.Printf("失败,断言的类型为%T,而i的类型为%T", t, i)
  os.Exit(1)
} 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. IDEA+Maven+多个Module模块(创建多模块SpringBoot整合项目)

    最近在学习springboot,先从创建项目开始,一般项目都是一个项目下会有多个模块,这里先创建一个最简单的实例,一个项目下有一个springboot模块项目提供web服务,引用另一个java项目(相 ...

  2. 图片文字识别aip的一个小Demo

    目前接触到了一个新的内容,识别图片上的文字,以下是这个Demo 首先需要在需要在百度云-管理中心创建应用 地址:http://console.bce.baidu.com/ai/#/ai/ocr/app ...

  3. MT【290】内外圆求三角最值

    求$\sqrt{\dfrac{5}{4}-\sin x}+2\sqrt{\dfrac{9}{4}+\cos x-\sin x}$的最小值. 提示:$\sqrt{\dfrac{5}{4}-\sin x} ...

  4. MT【273】2014新课标压轴题之$\ln2$的估计

    已知函数$f(x)=e^x-e^{-x}-2x$(1)讨论$f(x)$的单调性;(2)设$g(x)=f(2x)-4bf(x),$当$x>0$时,$g(x)>0,$求$b$的最大值;(3)已 ...

  5. 【BZOJ5318】[JSOI2018]扫地机器人(动态规划)

    [BZOJ5318][JSOI2018]扫地机器人(动态规划) 题面 BZOJ 洛谷 题解 神仙题.不会.... 先考虑如果一个点走向了其下方的点,那么其右侧的点因为要被访问到,所以必定只能从其右上方 ...

  6. HGOI 20190310 题解

    /* 又是又双叒叕WA的一天... 我太弱鸡了... 今天上午打了4道CF */ Problem 1 meaning 给出q组询问,求下列函数的值$ f(a) = \max\limits_{0 < ...

  7. bzoj2560串珠子(子集dp)

    铭铭有n个十分漂亮的珠子和若干根颜色不同的绳子.现在铭铭想用绳子把所有的珠子连接成一个整体. 现在已知所有珠子互不相同,用整数1到n编号.对于第i个珠子和第j个珠子,可以选择不用绳子连接,或者在ci, ...

  8. centos7添加并挂载新硬盘

    环境目标: 配置一台centos7,主硬盘40G装系统:副硬盘200G作为数据盘(格式:XFS)挂载到根目录:/data/ 说明:XFS是高性能文件系统,SGI为他们的 IRIX平台而设计: 自从20 ...

  9. Cookie知识点总结

    Cookie机制是采用客户端保持Http状态信息的方案. Cookie是在浏览器访问web服务器的某个资源的时候,由web服务器在http响应消息头中附带给浏览器的一个小文本文件. 一旦web服务器保 ...

  10. ICPC模板编写 latex

    推荐模板 https://www.jianshu.com/p/28552706e811 另外导入C文件: \lstinputlisting[language=C]{xxx.c} .c文件编码方式为UT ...