编译运行下面的代码后,在浏览器中测试下面的url,然后看服务端输出的效果:
http://127.0.0.1:9090/ http://127.0.0.1:9090/?name=test&&age=25 http://127.0.0.1:9090/?name=test&&age=25&&url_long=helloserver http://127.0.0.1:9090/?name=test&&age=25&&url_long=helloserver&&url_long=othertest
package main import ( "fmt" "log" "net/http" "strings" ) func sayhelloname(w http.ResponseWriter, r *http.Request) { r.ParseForm() //解析参数 fmt.Println(r.Form) fmt.Println("path:", r.URL.Path) fmt.Println("scheme:", r.URL.Scheme) fmt.Println(r.Form["url_long"]) for k, v := range r.Form { fmt.Println("key:", k) fmt.Println("value:", strings.Join(v, "")) } fmt.Fprintf(w, "hello, welcome you!") //这个字符串写入到w中,用于返回给客户端。 } func main() { http.HandleFunc("/", sayhelloname) //设置访问的路由 err := http.ListenAndServe(":9090", nil) //设置监听的端口 if err != nil { log.Fatal("ListenAndServe: ", err) } }