This is a creation in Article, where the information may have evolved or changed.
Package Mainimport (//"FMT" "io" "Log" "Net/http" "OS" "RegExp" "Time") Func main () {servemux: = http. Newservemux () Servemux.handlefunc ("/", SayHello) Servemux.handlefunc ("/bye", Saybye)//Servemux.handlefunc ("/static ", staticserver) Server: = http. SERVER{ADDR: ": 8080", Handler: servemux,readtimeout:5 * time. Second,}err: = Server. Listenandserve () if err! = Nil {log. Fatal (Err)}}func SayHello (w http. Responsewriter, R *http. Request) {If ok, _: = RegExp. Matchstring ("/static/", R.url. String ()); OK {staticserver (W, R) Return}io. WriteString (W, "Hello World")}func Saybye (w http. Responsewriter, R *http. Request) {io. WriteString (W, "Byebye")}func Staticserver (w http. Responsewriter, R *http. Request) {WD, err: = OS. GETWD () if err! = Nil {log. Fatal (Err)}http. Stripprefix ("/static/", http. Fileserver (http. Dir (WD)). Servehttp (W, R)}
http. Listenandserve (addr string, Handler handler)//Handlerhttp. Handlerinterface (servehttp)//Handler single implements servehttp//http. Listenandserve (": 8080", single) so this//Newservemuxhttp.servemux implements servehttp//http. Listenandserve (": 8080", MUX) so you can do this//HTTP. The server contains handler, or HTTP. The defaultservemux//Defaultservemuxservemux implements servehttp//http. Listenandserve (addr string, Handler handler)//Bottom http.server//call Server. Listenandserve ()//So the last Call//Http.server structure under the Listenandserve () method//parameter addr, handler mainly for the assignment//main method handler under Servehttp side Law//responsible for general control