多态。struct 可以赋值给 interface。interface 可以转换成子接口,或者 struct。

请看go中的一段的源代码:

listener, _ := net.Listen("tcp", "localhost:8000")
tcpListener := listener.(*net.TCPListener)
conn,_ := tcpListener.Accept()

仔细拜读源码可知:

net.Listen() 返回了一个 Listener接口,而 *TCPListener本身实现了该接口,所以可以转换成 TCPListener,再调用其Accept()。

实际上,并没有必要显示地转换:

listener, _ := net.Listen("tcp", "localhost:8000")
conn,_ := listener.Accept()

由源码可知,Listen(tcp,url) 函数实际返回的是 *TCPListener  或者 *UnixListener,只不过返回的值赋给了 Listener接口。而在调用该接口的 Accept()方法时,为TCPListener  或者 UnixListener的方法。

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

    接口定义 Interface类型可以定义一组方法,但是这些不需要实现.并且interface不能 包含任何变量. type Interface interface { test1(a, b int) ...

  5. golang interface判断为空nil

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

  6. 浅析 golang interface 实现原理

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

  7. golang interface 多态

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

  8. golang interface 类型学习

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

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

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

随机推荐

  1. ui-router 1.0 001 - resolve, component, sref-active

    特性介绍: state支持component形式 state的Resolve配置可以在激活state之前先获取数据, 绑定给component ui-sref-active="active& ...

  2. C++ 获取程序编译时间

    一个简单的需求,就是需要程序判断当前系统的时间是不是在程序编译之后的,如果系统当前时间在编译之前,那说明这台机器的时间是不正确的,需要终止程序运行. 因为要在程序编译时候获取时间,如果每次编译前手动修 ...

  3. gitlab Docker容器创建命令以及从容器中备份gitlab仓库示例

    Gitlab容器启动命令: docker run -d --name gitlab --publish : --publish : --hostname gitlab-server --volume ...

  4. Windows批处理 调用程序后 不等待子进程 父进程继续执行命令

    从DOS过来的老鸟应该都知道批处理,这个功能在WINDOWS中仍然保留着.批处理 说白了就是把一系列DOS命令写在一个文本文件里,然后把这个文件命名为XXX.bat(WINXP以后的系统也可以命名为* ...

  5. JavaScript绑定this

    问题描述 var a = { one: 1, haha() { console.log(this.one) } } setTimeout(a.haha, 1000) 在上例中,函数haha引用了thi ...

  6. Java中使用FileputStream导致中文乱码问题的修改方案

    package com.pocketdigi; import java.io.File; import java.io.FileInputStream; import java.io.FileOutp ...

  7. android下使用tcpdump抓包

    tcpdump是linux下的抓包工具,在android中没有,需要下载对应的工具. 下载地址:https://www.androidtcpdump.com/android-tcpdump/downl ...

  8. More than the maximum number of request parameters

    前些时间,我们的的一个管理系统出现了点问题,原本运行的好好的功能,业务方突然讲不行了,那个应用已经运行了好多年了,并且对应的代码最近谁也没改动过,好奇怪的问题,为了解决此问题,我们查看了日志,发现请求 ...

  9. [转]protoc-gen-lua 编译、安装、使用教程

    版权声明:本文转自http://blog.csdn.net/huutu 转载请带上 http://www.liveslives.com/ https://blog.csdn.net/cp7906216 ...

  10. django 自定义数据校验

    http://stackoverflow.com/questions/16231183/django-how-to-override-clean-method-in-a-subclass-of-cus ...