在 go 网络库 net/http 的请求处理过程 中我们解析了 net/http 的请求处理流程。并说到所有基于 net/http 的 web 框架定制的其实就是 Server 中的下面几个部分: Handler: 实现更丰富的路由 Context: 实现更
另一个路由实现 https://github.com/gorilla/mux
1. 启用 https 服务 1.1 服务器端 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!\n") }) // 启用 https 服务 fmt.Println(http.ListenAndServeTLS("localhost:8081", "../server-signed-by-ca.crt", "../server.key", nil)) // 启用 http 服务 // http.ListenAndServe("localhost:8080", nil) } 1.2
1. net/http 库结构 我们先来看一个最简单的 http server: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package web import ( "fmt" "log" "net/http" ) func StartWebServer() { http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "hello, %q", "tsong") }) http.ListenAndServe(":8080", nil) } // net/http 库 func ListenAndServe(addr string, handler
1. httprouter 简介 在 go 网络库 net/http 的请求处理过程 中我们了解了标准库 net/http 的请求处理流程。net/http 中提供了一个默认的路由实现 ServeMux,通过一个 map
go 执行操作系统命令