Go 接口转换的一个例子
今天将Go 的websocket集成到已有的http中时也遇到以下一个问题:其中主要是接口类型转换
转载自[http://www.cnblogs.com/ghj1976/archive/2013/04/23/3037914.html]
我们用Golang做网站时,必然会有这样的代码:
http.Handle("/", http.FileServer(http.Dir(".")))
注意这里的 http.Handle( 函数的第二个参数,Golang的源码中这个函数的描述如下:
// Handle registers the handler for the given pattern
// in the DefaultServeMux.
// The documentation for ServeMux explains how patterns are matched.
func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }
其中第二个参数是一个 interface,必须实现ServeHTTP(ResponseWriter, *Request)方法
// Objects implementing the Handler interface can be
// registered to serve a particular path or subtree
// in the HTTP server.
//
// ServeHTTP should write reply headers and data to the ResponseWriter
// and then return. Returning signals that the request is finished
// and that the HTTP server can move on to the next request on
// the connection.
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
如果我们用WebSocket时,又会发现, 我们给http.Handle( 传递的是 websocket.Handler(。
http.Handle("/socket", websocket.Handler(Echo))
今天在将WebSocket集成到已有的http
websocket.Handler 又是另外一个接口,如下
// Handler is an interface to a WebSocket.
type Handler func(*Conn)
这里为啥会出现接口更换了? 其实 再往下看可以看到 websocket.Handler 接口多一个公共的方法:ServeHTTP, 即接口 websocket.Handler 实现了 http 的 Handler 接口。
// ServeHTTP implements the http.Handler interface for a Web Socket
func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
这里的interface有了其他语言类的一些特征,比较有趣。
// Handler is an interface to a WebSocket.type Handler func(*Conn)
// ServeHTTP implements the http.Handler interface for a Web Socketfunc (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)
type websocket.Handler func(*websocket.Conn)
Handler is a simple interface to a WebSocket browser client. It checks if Origin header is valid URL by default. You might want to verify websocket.Conn.Config().Origin in the func. If you use Server instead of Handler, you could call websocket.Origin and check the origin in your Handshake func. So, if you want to accept non-browser client, which doesn't send Origin header, you could use Server . that doesn't check origin in its Handshake.
- method
(websocket.Handler).ServeHTTP(w net/http.ResponseWriter, req *net/http.Request)
通过以前的信息可以看出websocket.Handler实现了http.Handler接口,从而实现了接口类型的转换。
Go 接口转换的一个例子的更多相关文章
- 一个vue请求接口渲染页面的例子
new Vue({ el:'#bodylist', data: { list: [ { "type_id": "1", "type_name" ...
- 对Jena的简单理解和一个例子
本文简单介绍Jena(Jena 2.4),使用Protégé 3.1(不是最新版本)创建一个简单的生物(Creature)本体,然后参照Jena文档中的一个例子对本体进行简单的处理,输出本体中的Cla ...
- spring笔记--使用springAPI以及自定义类 实现AOP的一个例子
Spring的另一个重要思想是AOP,面向切面的编程,它提供了一种机制,可以在执行业务前后执行另外的代码,Servlet中的Filter就是一种AOP思想的体现,下面通过一个例子来感受一下. 假设我们 ...
- 从一个例子中体会React的基本面
[起初的准备工作] npm init npm install --save react react-dom npm install --save-dev html-webpack-plugin web ...
- Erlang 程序引发共享内存 bug 的一个例子
虽然 Erlang 的广告说得非常好,functional.share-nothing.消息传递,blah blah 的,好像用 Erlang 写并发程序就高枕无忧了,但是由于 Erlang 信奉高度 ...
- Spark小课堂Week7 从Spark中一个例子看面向对象设计
Spark小课堂Week7 从Spark中一个例子看面向对象设计 今天我们讨论了个问题,来设计一个Spark中的常用功能. 功能描述:数据源是一切处理的源头,这次要实现下加载数据源的方法load() ...
- Webpack入门——使用Webpack打包Angular项目的一个例子
2016.1.22,对大多数人来说,这是一个非常平常的日子,但这却是我决定在博客园写博客的日子.虽然注册博客园的博客已有4年8个月,却一直没有动手写过一篇博客,原因是觉得自己水平不行,写不出好东西,所 ...
- Web服务,XFire的一个例子
Web服务优点 互操作性:实现不同系统间的相互调用(语言无关.平台无关) Web服务是什么 Web 服务是一类应用程序,是能够用编程的方法通过Web调用来实现某个功能的应用程序 Web服务的体系结构 ...
- Java编程思想中关于闭包的一个例子
Java编程思想中的一个例子,不是很理解使用闭包的必要性,如果不使用闭包,是不是有些任务就不能完成?继续探索. package InnerClass; interface Incrementable ...
随机推荐
- STM8的wait for interrupt
如果我用disable interrupt和enable interrupt包裹wait forinterrupt(WFI).你说WFI还能被唤醒么?有思考过么? 昨晚拿STM8L151K4的开发板, ...
- Oracle非重要文件恢复,redo、暂时文件、索引文件、password文件
增量备份的应用在recovery阶段.不再restore阶段 了解数据库设置表: SQL>desc database_properties Name ...
- SimpleTagImageView
https://github.com/wujingchao/SimpleTagImageView SimpleTagImageView ImageView with a tag in android. ...
- Python dictionary implementation
Python dictionary implementation http://www.laurentluce.com/posts/python-dictionary-implementation/ ...
- C++11 类内初始化
C++11新标准规定,可以为数据成员提供一个类内初始值.创建对象时,类内初始值将用于初始化数据成员.没有初始值的成员将默认初始化. 对类内初始值的限制与之前介绍的类似:或者放在花括号里,或者放在等号右 ...
- PAT 1021
1021. Deepest Root (25) A graph which is connected and acyclic can be considered a tree. The height ...
- apache vhost 访问权限配置
apache的<directory> </directory>语句,查考如下: 如何访问根目录下的目录http://192.168.1.12/test/ 第一.缺省ap ...
- 聊聊 #pragma 和 // MARK:
我去,就这两个东西还要讲?是OC或Swift开发人员都知道是怎么回事好吗?不就是用来标记和分组代码的吗?难道还有别的装逼技能? 当然,其实问大部分人说这两个是什么作用,或者是除了这两个还知道什么的情况 ...
- learning nodejs 1 - stream.pipe
a simple http server using inner http module. var http = require('http'); var fs = require('fs'); // ...
- Linux 2.6的内核编译过程
由于上课需要,老师指定使用linux-2.6.26内核.本人是菜鸟级别的. 由于本人安装的ubuntu系统中,没有自带GCC,故需要自己安装gcc, 首先启用ROOT权限 输入:sudo -i 获取 ...