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 ...
随机推荐
- C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
C# 强制关闭当前程序进程(完全Kill掉不留痕迹) /// <summary> /// 运行DOS命令 /// DOS关闭进程命令(ntsd -c q -p PID )PID为进程的ID ...
- 【每日一摩斯】-Index Skip Scan Feature (212391.1)
INDEX Skip Scan,也就是索引快速扫描,一般是指谓词中不带复合索引第一列,但扫描索引块要快于扫描表的数据块,此时CBO会选择INDEX SS的方式. 官方讲的,这个概念也好理解,如果将复合 ...
- eclipse中Build Path-Add to Build Path相应到androidstudio的设置
有些时候并不须要加入lib库进行编译,比如在使用xposed的jar包时,仅仅须要在eclipse里Build Path-Add to Build Path就可以,假设作为lib库加入进去反而会出现异 ...
- 获取文件属性信息之stat、fstat和lstat
UNIX文件系统是目录和文件组成的一种层次结构.目录(directory)是一个包含许多目录项的文件,在逻辑上,可以认为每个目录项都包含一个文件名,同时还包含说明该文件属性的信息.文件属性是指文件类型 ...
- python邮件发送接收
接收邮件 import poplib,pdb,email,re,time from email import header POP_ADDR = r'pop.126.com' USER = '' PA ...
- struts2.1笔记05:struts2开发环境的搭建
1.找到开发Struts应用需要使用到的jar文件. 首先我们要在myEclipse中新建一个Web Project,我们这里命名为"struts2".然后我们就要使用jar文件, ...
- CSS完成三角形
通过设置div的border属性,来实现! 将div的height和width属性分别设置为0px,将4边的border值设置为一样的宽度,通过调节边框的颜色课看到效果. <style> ...
- weblogic服务器内存溢出问题解决
问题描述: 当我们在weblogic服务器一个域domain上面部署多个工程时,经常会出现内存溢出的情况:java.lang.OutOfMemoryError异常 原因:主要是因为weblogic环境 ...
- webservice发布接口
一:编写接口程序,计算功能类,有加减乘除四个方法 /** * */ package com.hlcui.util; /** * @author Administrator 将此类发布为公共接口 */ ...
- 转:Nginx+ffmpeg的HLS开源服务器搭建配置及开发详解
转:http://itindex.net/detail/51186-nginx-ffmpeg-hls 本文概述: 至目前为止,HLS 是移动平台上非常重要并十分流行的流媒体传输协议.做移动平台的流媒体 ...