go http

go http 分析

一曲冷凌霜 提交于 2019-11-29 14:27:43
原生http http.ListenAndServe handler 参数(w http.ResponseWriter, r *http.Request) go参数传递为值传递,request长用来获取参数等,所以直接传递指针比较好,而 ResponseWriter 是个接口,只要实现接口就行 无所谓传不传指针的问题。 package main import ( "net/http" "log" "io" ) func main() { http.Handle("/", sayHello) err := http.ListenAndServe(":8080", nil) if err != nil { log.Fatal(err) } } func sayHello(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "hello world, version 1") } //访问 http://localhost:8080/ http.ServeMux 更底层路由设置 http.ServeMux解析 ServeMux 其实是路由表,主要使用map结构,其实例必须实现 ServeHTTP() 方法 mux.m[pattern] = muxEntry{explicit: true, h: handler,