Go語言入門經典第18章
package main
import (
"net/http"
"fmt"
"io/ioutil"
)
func main() {
fmt.Println("start")
http.HandleFunc("/", hello)
http.ListenAndServe("0.0.0.0:8000", nil)
}
//curl -is "http://localhost:8000/?foo=1&bar=2"
//curl -is -X POST -d "POSTbody" http://localhost:8000/
//curl -is -X DELETE -d "DELETEss" http://localhost:8000/
func My2(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET" :
for k,v := range r.URL.Query() {
fmt.Printf("%s: %s\n", k, v)
}
w.Write([]byte("this is GET\n"))
case "POST" :
body, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println("post read err")
}
fmt.Printf("%s\n", body)
w.Write([]byte("this is POST\n"))
case "DELETE":
w.Write([]byte("this is DELETE\n"))
}
}
func My(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET" :
w.Write([]byte("this is GET\n"))
case "POST" :
w.Write([]byte("this is POST\n"))
}
}
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Method)
w.Header().Set("Qwe", "123")
w.Header().Set("Content-Type", "text/html;charset=utf-8")
w.Write([]byte("this is body hello\n"))
}
/*
[gdut17@localhost ~]$ curl -is http://localhost:8000
HTTP/1.1 200 OK
Qwe: 123
Date: Sat, 17 Oct 2020 11:52:10 GMT
Content-Length: 19
Content-Type: text/plain; charset=utf-8
this is body hello
[gdut17@localhost ~]$ curl -is -X POST http://localhost:8000
HTTP/1.1 200 OK
Date: Sat, 17 Oct 2020 11:58:09 GMT
Content-Length: 13
Content-Type: text/plain; charset=utf-8
this is POST
[gdut17@localhost ~]$ curl -is -X GET http://localhost:8000
HTTP/1.1 200 OK
Date: Sat, 17 Oct 2020 11:58:16 GMT
Content-Length: 12
Content-Type: text/plain; charset=utf-8
this is GET
[gdut17@localhost ~]$ curl -is "http://localhost:8000/?foo=1&bar=2"
HTTP/1.1 200 OK
Date: Sat, 17 Oct 2020 12:06:20 GMT
Content-Length: 12
Content-Type: text/plain; charset=utf-8
this is GET
[gdut17@localhost ~]$ curl -is -X POST -d "body" http://localhost:8000/
HTTP/1.1 200 OK
Date: Sat, 17 Oct 2020 12:07:38 GMT
Content-Length: 13
Content-Type: text/plain; charset=utf-8
this is POST
[gdut17@localhost ~]$ curl -is -X DELETE -d "DELETEss" http://localhost:8000/
HTTP/1.1 200 OK
Date: Sat, 17 Oct 2020 12:12:47 GMT
Content-Length: 15
Content-Type: text/plain; charset=utf-8
this is DELETE
[gdut17@localhost ~]$ curl -is http://localhost:8000
HTTP/1.1 200 OK
Content-Type: text/html;charset=utf-8
Qwe: 123
Date: Sat, 17 Oct 2020 12:14:58 GMT
Content-Length: 19
*/
相關文章
- C語言入門經典(第5版)C語言
- C語言入門經典(第4版)電子書pdf下載C語言
- JavaScript函數語言程式設計入門經典JavaScript函數程式設計
- Go語言快速入門Go
- ChainDesk : Go 語言入門指南AIGo
- [翻譯] Go 語言入門Go
- go語言快速入門教程Go
- 【Go語言入門系列】(八)Go語言是不是面嚮物件語言?Go物件
- 【Go by Example】GO語言入門 1-14Go
- GO 語言快速開發入門Go
- Go 語言入門教程:變數Go變數
- 【Go 語言入門專欄】Go 語言的起源與發展Go
- golang 快速入門 [3]-go 語言 helloworldGolang
- Go 語言基準測試入門Go
- Go語言程式設計快速入門Go程式設計
- Go語言快速入門筆記01Go筆記
- 如何入門GO語言?這份GO語言超詳細入門教程你值得擁有-千鋒Go
- csharp入門經典CSharp
- 【Go語言入門系列】(七)如何使用Go的方法?Go
- golang 快速入門 [1]-go 語言導論Golang
- go語言入門之-函式和方法Go函式
- GO語言入門 - (六)函式和方法Go函式
- 《Flutter 入門經典》之“Flutter 入門 ”Flutter
- Go 語言極速入門4 - 物件導向Go物件
- Go語言入門系列(四)之map的使用Go
- Go 語言入門練手專案推薦Go
- R語言經典統計分析R語言
- Python入門經典案例一Python
- Unix 入門經典 筆記筆記
- go語言入門教程分享:第一個程式:HelloWorldGo
- 願碼(ChainDesk.CN):Go語言入門指南(二)AIGo
- Go語言入門系列(六)之再探函式Go函式
- 你想要了解GO語言嗎?帶你入門!Go
- Go語言併發程式設計簡單入門Go程式設計
- Go語言的context包從放棄到入門GoContext
- 【搞定Go語言】第2天4:Go語言基礎之流程控制Go
- c語言入門C語言
- perl語言入門