十四、Web服务器

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "log"
 6     "net/http"
 7     "strings"
 8 )
 9 
10 //在浏览器输入http://localhost:9090
11 //可以看到浏览器页面输出了Hello astaxie!
12 //可以换一个地址试试:http://localhost:9090/?url_long1=111&url_long2=222
13 
14 func sayhelloName(w http.ResponseWriter, r *http.Request) {
15     r.ParseForm()       //解析参数,默认是不会解析的
16     fmt.Println(r.Form) //这些信息是输出到服务器端的打印信息
17     fmt.Println("path", r.URL.Path)
18     fmt.Println("scheme", r.URL.Scheme)
19     fmt.Println(r.Form["url_long"])
20     for k, v := range r.Form {
21         fmt.Println("key:", k)
22         fmt.Println("val:", strings.Join(v, ""))
23     }
24     fmt.Fprintf(w, "Hello astaxie!") //这个写入到w的是输出到客户端的
25 }
26 
27 func main() {
28     http.HandleFunc("/", sayhelloName)       //设置访问的路由
29     err := http.ListenAndServe(":9090", nil) //设置监听的端口
30     if err != nil {
31         log.Fatal("ListenAndServe: ", err)
32     }
33 }

結果:

map[url_long2:[222] url_long1:[111]]


path /


scheme 


[]


key: url_long1


val: 111


key: url_long2


val: 222


map[]


path /favicon.ico


scheme


[]

posted on 2013-02-25 10:54  liubiaoren  阅读(217)  评论(0)    收藏  举报