http-server.go
package main
import (
"fmt"
"io"
"net/http"
"strings"
)
// 處理函數
func helloserver(w http.ResponseWriter,r *http.Request) {
fmt.Println(r.URL.Path)
strs :=strings.Split(r.URL.Path,"/")
fmt.Println(strs)
io.WriteString(w,strs[1]+ "," +strs[2])
}
func main() {
// 1.設置路由,所有 /hello/* 都回去helloserver函數執行
http.HandleFunc("/hello/",helloserver)
// 2.啓動服務
http.ListenAndServe(":8080",nil)
}
訪問
http://localhost:8080/hello/xiaoming