go web部分 | 笔记整理!
Go web部分 | 笔记整理!
go web部分
Server
1 | // handler func(ResponseWriter, *Request)) |
1. HandleFunc
1 | // HandleFunc registers the handler function for the given pattern |
1 | type ServeMux struct { |
1 | // HandleFunc registers the handler function for the given pattern. |
1 | // Handle registers the handler for the given pattern. |
2. ListenAndServe
1 | // ListenAndServe listens on the TCP network address addr and then calls |
1 | type Server struct { |
1 | type Handler interface { |
模拟实现一下
1 | // handler func(ResponseWriter, *Request)) |
Dir
1 | // A Dir implements FileSystem using the native file system restricted to a |
FileServer
1 | package main |
2. FileServer
1 | // FileServer returns a handler that serves HTTP requests |
1 | type fileHandler struct { |
1 | // A FileSystem implements access to a collection of named files. |
1 | type Handler interface { |
1 | func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) { |
index
1 | // 处理函数 |
1. 添加cookie信息
设置cookie信息
1 | // 处理cookie |
验证cookie信息
1 | func session(w http.ResponseWriter, r *http.Request) (see data.Session, err error) { |
2. 处理优化
1 | func index(writer http.ResponseWriter, request *http.Request) { |
3. 继续抽离
1 | // 继续优化 |
1 | // 处理函数 |