Go 的 Http 库使用

Go 的 Http 库使用。~~~go http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {

配置及启动服务

1
2
3
4
5
6
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello World!"))
})
http.HandleFunc("/test", TestHandle)

err := http.ListenAndServe(":8080", nil)

传入参数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// 获取Header
header := r.Header
log.Println(header)

// 获取param参数
r.ParseForm()
log.Println(r.Form)

// 获取Form参数
r.ParseForm()
log.Println(r.PostForm)

// 获取Json参数
Body := json.NewDecoder(r.Body)
device := Device{}
Body.Decode(&device)
log.Println(device)

传出参数

Licensed under CC BY-NC-SA 4.0
Gear(夕照)的博客。记录开发、生活,以及一些不足为道的思考……