github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/coding/net/00-listenAndServe.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  )
     8  
     9  // 分析http.ListenAndServe机制
    10  func main() {
    11  	http.HandleFunc("/", indexHandler)
    12  	// 监听TCP地址
    13  	// handler通常设为nil,此时会使用DefaultServeMux(默认路由器)
    14  	log.Fatal(http.ListenAndServe(":9999", nil))
    15  }
    16  
    17  func indexHandler(w http.ResponseWriter, r *http.Request) {
    18  	fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
    19  }