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("val:", strings.Join(v, "v")) } fmt.Fprintf(w, "Hello astaxie") //这个写入到w的是输出到客户端的 } func main() { http.HandleFunc("/", sayHelloName) //设置访问的路由 err := http.ListenAndServe(":9090", nil) //设置监听的端口 if err != nil { log.Fatal("ListenAndServe", err) } }
cmd 到文件所在的位置 go build web.go生成web.exe,运行web.exe
在浏览器输入http://localhost:9090
可以看到浏览器页面输出了Hello astaxie!
map[] path / scheme [] map[] path /favicon.ico scheme []
http://localhost:9090/?url_long=111&url_long=222
command-line-arguments map[url_long:[111 222]] path / scheme [111 222] key: url_long val: 111v222 map[] path /favicon.ico scheme []
http://localhost:9090/?url_long=111&url_long=222&user=1
map[url_long:[111 222] user:[1]] path / scheme [111 222] key: url_long val: 111v222 key: user val: 1 map[] path /favicon.ico scheme []