package mainimport ("fmt""log""net/http""strings")func HttpHandle(rw http.ResponseWriter, req *http.Request) {req.ParseForm() //解析参数,默认是不会解析的for key, val := range req.Form {fmt.Println("-------------------------")fmt.Println("key:", key)fmt.Println("val:", strings.Join(val, ""))}
fmt.Fprintf(rw, "resp hello!")}func main() {http.HandleFunc("/", HttpHandle) //设置访问的路由err := http.ListenAndServe(":9080", nil) //设置监听的端口if err != nil {log.Fatal("ListenAndServe: ", err)}
}编译之后运行服务器端:在浏览器输入:http://localhost:9080/。就会在浏览器显示resp hello!也可以为服务器输入带参数:http://localhost:9080/?key1=val1&key2=val2&key3=val3
有疑问加站长微信联系(非本文作者)