Golang http 建立Web服务器

来源:互联网 发布:负反馈放大器实验数据 编辑:程序博客网 时间:2024/05/29 16:10

package mainimport ("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, ""))}fmt.Fprintf(w, "Hello astaxie!") //输出到客户端的信息}func main() {http.HandleFunc("/", sayhelloName)       //设置访问的路由err := http.ListenAndServe(":9090", nil) //设置监听的端口if err != nil {log.Fatal("ListenAndServe: ", err)}}

在浏览器中先后输入

http://localhost:9090

http://localhost:9090/?url_long=111&url_long=222

输出到浏览器的内容是: Hello astaxie!

服务器端的控制台的输出:

G:\Users\chenjo>go run web.go

map[]

path /

scheme

[]

map[]

path /favicon.ico

scheme

[]

map[url_long:[111 222]]

path /

scheme

[111 222]

key: url_long

val: 111222

map[]

path /favicon.ico

scheme

[]


原创粉丝点击