多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
**注册 Request Handler** ~~~go func (w http.ResponseWriter, r *http.Request) ~~~ 这个函数接收两个参数: http.ResponseWriter:text/html响应 http.Request:包含关于这个HTTP请求的所有信息,包括像URL或header simple demo ~~~go http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, you've requested: %s\n", r.URL.Path) }) ~~~ 一个request handler 无法接受外部HTTP的请求,需要一个监听某个端口的http server来处理request,将其传给对应的request handler。 ~~~go http.ListenAndServe(":80", nil) ~~~ <details> <summary> Demo </summary> ``` package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, you've requested: %s\n", r.URL.Path) }) http.ListenAndServe(":80", nil) } ``` </details> ***** **http.FileServer** ``` fs := http.FileServer(http.Dir("static/")) http.Handle("/static/", http.StripPrefix("/static/", fs)) ```