首先通过一个函数启动一个服务器,只提供一个方法并返回Hello World!,当你在浏览器输入http://127.0.0.1:8080,就会看到Hello World

对于http.ListenAndServe来说,需要我们提供一个Addr和一个Handler,所以当我们使用Hello实现了HandlerServeHTTP方法后,Hello就会被认为是一个Handler,并将其提供给http.ListenAndServe后会自动调用ServeHTTP方法。

type Hello struct {
} func (hello Hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
} func main() {
http.ListenAndServe(":8080", Hello{})
}

根据上图我们看到httpListenAndServe调用了Server中的ListenAndServe方法,所以以上代码可以写成,再次使用浏览器访问http://127.0.0.1:8080,我们看到了Hello World

type Hello struct {
} func (hello Hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
} func main() {
server := http.Server{Addr: ":8080", Handler: Hello{}}
server.ListenAndServe()
}

以上的方法我们只能通过根路径访问我们想要的结果,如何使用不同的路径访问不同的处理程序呢,你可以写成下面这样,使用http的Handle方法,把路径和程序关联起来并注册到程序中。使用浏览器访问http://127.0.0.1:8080/hello,我们看到了Hello World,那如果访问http://127.0.0.1:8080会是什么呢,我们看一下,是404 page not found

type Hello struct {
} func (hello Hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
} func main() {
http.Handle("/hello", Hello{})
http.ListenAndServe(":8080", nil)
}

好的,我们继续看httpHandle做了什么,点开源码,发现它使用DefaultServeMux调用了一个Handle函数

func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }

DefaultServeMux是什么,我们继续展开,发现DefaultServeMuxServeMux实例的一个指针,而且ServeMux是一个struct,通过Handle方法把程序路径和程序进行注册。

// DefaultServeMux is the default ServeMux used by Serve.
var DefaultServeMux = &defaultServeMux var defaultServeMux ServeMux // Handle registers the handler for the given pattern.
// If a handler already exists for pattern, Handle panics.
func (mux *ServeMux) Handle(pattern string, handler Handler) {
mux.mu.Lock()
defer mux.mu.Unlock() if pattern == "" {
panic("http: invalid pattern")
}
if handler == nil {
panic("http: nil handler")
}
if _, exist := mux.m[pattern]; exist {
panic("http: multiple registrations for " + pattern)
} if mux.m == nil {
mux.m = make(map[string]muxEntry)
}
e := muxEntry{h: handler, pattern: pattern}
mux.m[pattern] = e
if pattern[len(pattern)-1] == '/' {
mux.es = appendSorted(mux.es, e)
} if pattern[0] != '/' {
mux.hosts = true
}
}

继续观察这个struct,发现它实现了ServeHTTP,那也就是说明ServeMux是一个Handler

// ServeHTTP dispatches the request to the handler whose
// pattern most closely matches the request URL.
func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
if r.RequestURI == "*" {
if r.ProtoAtLeast(1, 1) {
w.Header().Set("Connection", "close")
}
w.WriteHeader(StatusBadRequest)
return
}
h, _ := mux.Handler(r)
h.ServeHTTP(w, r)
}

根据以上分析发现DefaultServeMux是一个默认的路由程序,它将接收到的不同的程序映射到不同的路径上等待访问,同时它也是一个Handler

现在我把程序变动一下,这样也能达到同样的效果, 使用浏览器访问http://127.0.0.1:8080/hello依然能看到Hello World

func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
})
http.ListenAndServe(":8080", nil)
}

http中的HandleFunc又是什么呢,我们点进去看一下,发现这个方法需要传入func(ResponseWriter, *Request)类型的函数。并且最终被传入了我们前面提到的ServeMuxHandle中。但是,我们知道只有实现了ServeHTTP方法才是Handler,而ServeMuxHandle需要一个Handler类型的变量,显然这个handler函数并不是一个真正的Handler

// HandleFunc registers the handler function for the given pattern.
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
if handler == nil {
panic("http: nil handler")
}
mux.Handle(pattern, HandlerFunc(handler))
}

但是我们看到它被强转成了HandlerFunc类型。那么我们继续查看HandlerFuncHandlerFunc实现了ServeHTTP,那么这就说明HandlerFunc是一个Handler,所以最终传入到ServeMux.Handle中的HandlerFunc(handler)是一个Handler

// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request) // ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
f(w, r)
}

那么我们的代码也可以写成这样:

func Hello(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
} func main() {
http.HandleFunc("/hello", Hello)
http.ListenAndServe(":8080", nil)
}

或者这样:

func Hello(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
} func main() {
http.Handle("/hello",http.HandlerFunc(Hello))
http.ListenAndServe(":8080", nil)
}

最后http包中还有几个内置的Handler,您可以自己去探索:

NotFoundHandler
StripPrefix
RedirectHandler
TimeoutHandler
FileServer

Handle详解的更多相关文章

  1. android handle详解3 ThreadHandler

    在android handle详解2的基础上,我们来学习ThreadHandler ThreadHandler的本质就是对android handle详解2的实现 HandlerThread其实还是一 ...

  2. android handle详解2 主线程给子线程发送消息

    按照android handler详解分析的原理我们可以知道,在主线程中创建handle对象的时候,主线程默认创建了一个loop对象使用threalocal函数将loop对象和主线程绑定. 我们能不能 ...

  3. android handle详解

    我们来看一个简单的代码: package com.mly.panhouye.handlerdemo; import android.content.Intent; import android.os. ...

  4. Android中Handle详解

    上图为本人总结的Handler,网上发现一片总结很好的博客就copy过来:作为参考 Handler有何作用?如何使用? 一 .Handler作用和概念 包含线程队列和消息队列,实现异步的消息处理机制, ...

  5. Node.js npm 详解

    一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...

  6. JavaScript事件详解-jQuery的事件实现(三)

    正文 本文所涉及到的jQuery版本是3.1.1,可以在压缩包中找到event模块.该篇算是阅读笔记,jQuery代码太长.... Dean Edward的addEvent.js 相对于zepto的e ...

  7. JavaScript事件详解-Zepto的事件实现(二)【新增fastclick阅读笔记】

    正文 作者打字速度实在不咋地,源码部分就用图片代替了,都是截图,本文讲解的Zepto版本是1.2.0,在该版本中的event模块与1.1.6基本一致.此文的fastclick理解上在看过博客园各个大神 ...

  8. @RequestMapping 用法详解之地址映射

    @RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...

  9. 详解Javascript的继承实现(二)

    上文<详解Javascript的继承实现>介绍了一个通用的继承库,基于该库,可以快速构建带继承关系和静态成员的javascript类,好使用也好理解,额外的好处是,如果所有类都用这种库来构 ...

随机推荐

  1. 鸿蒙运行报错:Failure[INSTALL_PARSE_FAILED_USESDK_ERROR] Error while Deploying HAP

    问题描述 近期,使用DevEco-Studio新建手机类型的工程,编译成功,发布到模拟器(鸿蒙P40)时出错,如下图: 原因分析 本地DevEco-Studio使用的SDK版本与设备(P40)不匹配导 ...

  2. 冒泡排序(BubbleSort)

    介绍: 冒泡排序是一种最基础的交换排序(两两比较待排序的关键字,交换不满足次序要求的那对数,直到整个表都满足次序要求为止),工作方式如同碳酸饮料中的二氧化碳气泡最终会上浮到顶端一样,故名"冒 ...

  3. 《环形队列》游戏高《TPS》模式下减少cpu线程切换

    序言 什么高TPS?QPS,其实很多人都知道,还有人说大数据,大流量这些关键词夜以继日的出现在我们眼前: 针对高TPS,QPS这些词汇还有一个次可能比较陌生那就是CCU,tps,qps接受满天飞,CC ...

  4. [Fundamental of Power Electronics]-PART I-5.不连续导电模式-5.3 Boost变换器实例

    5.3 Boost变换器实例 作为第二个示例,考虑图5.12的Boost变换器.让我们来确定不同模式的边界并且求解DCM下的电压变换比.此前在2.3节中分析了在CCM工作的Boost变换器的特性,并确 ...

  5. DSP代码搬运至RAM运行

    程序运行过程中,有些函数或程序段和数据等经常调用,正常情况下在FLASH中运行处理消耗时间和资源较大,通常将其移植至RAM中运行,可提高运行效率. 如: 1 #pragma CODE_SECTION( ...

  6. JWT 介绍 - Step by Step

    翻译自 Mohamad Lawand 2021年3月11日的文章 <Intro to JWT - Step by Step> [1] 在本文中,我将向您介绍 JWT[2]. 我们今天要讲的 ...

  7. babel配置文件.babelrc详解

    一:理解 babel之配置文件.babelrc 基本配置项 1. 什么是babel? 它是干什么用的? ES6是2015年发布的下一代javascript语言标准,它引入了新的语法和API,使我们编写 ...

  8. IDEA常用个性化设置

    自己对于IDEA一些个性化设置,这里简单记录一下,省的之后四处百度 常用插件安装 CodeGlance:代码编写区域右侧小地图 GenerateAllSetter:针对new出来的实体类对象快速调用G ...

  9. zabbix容器化安装及监控docker应用

    一.zabbix agent2 介绍 从Zabbix 4.4之后,官方推出了Zabbix Agent 2,意味着zabbix 不在只是物理机监控的代名词,现在你可以使用Go为Zabbix编写插件,来监 ...

  10. 深入探究ASP.NET Core读取Request.Body的正确方式

    前言 相信大家在使用ASP.NET Core进行开发的时候,肯定会涉及到读取Request.Body的场景,毕竟我们大部分的POST请求都是将数据存放到Http的Body当中.因为笔者日常开发所使用的 ...