内容简介:作者:施洪宝使用示例(net/http包中还有其他方式构建http服务, 具体情况参考官方文档)
作者:施洪宝
一. 介绍
-
1.codis使用了 go 中martini这个web框架
- martinie github地址: https://github.com/go-martini...
- 2.martini主要是利用go标准库中的net包以及net/http包
二. net包
-
1.net包文档
- 2.官方文档中已经说明了如何使用net包实现tcp服务端监听
ln, err := net.Listen("tcp", ":8080")
if err != nil {
// handle error
}
for {
conn, err := ln.Accept()
if err != nil {
// handle error
}
go handleConnection(conn)
}
- 从使用库的一方来看, 接受连接的操作是阻塞式的, 使用方只需按照阻塞式网络编程进行处理即可
- 由于go有自己的协程调度, 在接受网络连接时, 实际使用的是IO多路复用, 具体则是通过sysmon线程进行操作
- 由于go具有垃圾自动回收以及协程, 故而针对每个网络请求, 开启单独协程进行处理
三. net/http包
-
-
对于http服务, go提供了net/http包可以直接使用
-
使用示例(net/http包中还有其他方式构建http服务, 具体情况参考官方文档)
http.Handle("/foo", fooHandler)
http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
log.Fatal(http.ListenAndServe(":8080", nil))
- http.Handle, http.HandleFunc用于向DefaultServeMux注册处理函数
- http.ListenAndServe用于启动http服务, 当第二个参数为nil时, 表示使用默认的DefaultServeMux处理http请求
3.1 源码简介
- 1.go采用1.11.4, 不同的版本实现可能不同, 源码参见src/net/http/server.go
- 2.结构体定义
type ServeMux struct {
mu sync.RWMutex
m map[string]muxEntry
hosts bool // whether any patterns contain hostnames
}
var DefaultServeMux = &defaultServeMux
var defaultServeMux ServeMux
//http请求处理接口
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
- 3.函数定义
func Handle(pattern string, handler Handler) func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) func ListenAndServe(addr string, handler Handler) error
-
4.基本流程
- 通过http.Handle或者http.HandleFunc, 向DefaultServeMux中注册一些url相关的处理函数
- 通过http.ListenAndServe启动web服务
- 客户端发送http请求后, 服务端接受请求, 创建协程处理请求(找到最匹配的url并调用之前设置的处理函数)
以上所述就是小编给大家介绍的《【每日笔记】【Go学习笔记】2019-01-16 go网络编程》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 《UNIX网络编程》笔记 - select和poll
- 《UNIX网络编程》笔记 - 套接字选项/UDP套接字
- 网络编程——打开网络库
- 【go网络编程】-HTTP编程
- 【go网络编程】-Socket编程
- Python 网络编程
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Beginning Apache Struts
Arnold Doray / Apress / 2006-02-20 / USD 44.99
Beginning Apache Struts will provide you a working knowledge of Apache Struts 1.2. This book is ideal for you Java programmers who have some JSP familiarity, but little or no prior experience with Ser......一起来看看 《Beginning Apache Struts》 这本书的介绍吧!