// HandleFunc registers the handler function for the given pattern// in the DefaultServeMux.// The documentation for ServeMux explains how patterns are matched.funcHandleFunc(pattern string, handler func(ResponseWriter,*Request)){
DefaultServeMux.HandleFunc(pattern, handler)}
ListenAndServer实现了监听服务
// ListenAndServe always returns a non-nil error.funcListenAndServe(addr string, handler Handler)error{
server :=&Server{Addr: addr, Handler: handler}return server.ListenAndServe()}
代码示例
package main
import"fmt"import"net/http"funcwelcome(res http.ResponseWriter, req *http.Request){
fmt.Fprintln(res,"Hello World Golang Web,哈哈")}funcmain(){
http.HandleFunc("/", welcome)
http.ListenAndServe("localhost:8090",nil)
fmt.Println("服务已启动")//此行代码不执行,上面代码一直在监听localhost:8090端口}