github.com/qiniu/http 分析

相对于其他语言的 http service 框架,例如 Node.js 的 Express 和 Python 的 Flask,Go 作为网络C语言提供的 net/http 模块原生支持 http service。

在这里插入图片描述

package main

import (
    "io"
    "net/http"
)
func main() {
    http.HandleFunc("/", helloWorldHandler)
    http.ListenAndServe(":80", nil)
}
func helloWorldHandler(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "Hello world!")
}
Client -> Request -> Multiplexer(Router) -> Handler -> Response -> Client

创建一个http服务,大致需要经历两个过程,首先需要注册路由,即提供url模式和handler函数的映射,其次就是实例化一个server对象,并开启对客户端的监听。

参考资料

  1. 七牛如何做HTTP服务测试
  2. Golang开启http服务的三种方式
  3. https://github.com/speedwheel/awesome-go-web-frameworks
  4. 6 款最棒的 Go 语言 Web 框架简介
  5. 深入理解Golang之http server
发布了105 篇原创文章 · 获赞 230 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/aggresss/article/details/105155618