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語言程式設計入門經典18個小程式C語言程式設計
- C語言入門經典(第4版)電子書pdf下載C語言
- 《jQueryMobile入門經典》——第2章 使用HTML、CSS和JavaScriptjQueryHTMLCSSJavaScript
- Go語言經典筆試題Go筆試
- 《C語言入門經典》讀後感(一)C語言
- 程式語言入門經典案例【Python版】Python
- JavaScript函數語言程式設計入門經典JavaScript函數程式設計
- Go語言快速入門Go
- Storm入門之第7章使用非JVM語言開發ORMJVM
- [翻譯] Go 語言入門Go
- go語言快速入門教程Go
- ChainDesk : Go 語言入門指南AIGo
- 演算法競賽入門經典(第2版)-劉汝佳-第三章解題原始碼(C語言)演算法原始碼C語言
- 第09章 Go語言併發,Golang併發Golang
- 【Go語言入門系列】(八)Go語言是不是面嚮物件語言?Go物件
- 【Go by Example】GO語言入門 1-14Go
- GO 語言快速開發入門Go
- Go 語言入門教程:變數Go變數
- golang 快速入門 [3]-go 語言 helloworldGolang
- Go語言快速入門筆記01Go筆記
- 如何入門GO語言?這份GO語言超詳細入門教程你值得擁有-千鋒Go
- Webpack經典入門Web
- 【Go語言入門系列】(七)如何使用Go的方法?Go
- 《C語言開發從入門到精通》一第1章C語言之定位1.1 C語言的誕生C語言
- 第 12 章 CSS 入門CSS
- golang 快速入門 [1]-go 語言導論Golang
- go語言入門之-函式和方法Go函式
- GO語言入門 - (六)函式和方法Go函式
- 《Flutter 入門經典》之“Flutter 入門 ”Flutter
- R語言經典例項R語言
- 《HTML與CSS入門經典(第8版)》——2.8作業HTMLCSS
- 《HTML與CSS入門經典(第8版)》——1.6總結HTMLCSS
- [C語言] 第一章|C語言入門第一課C語言
- [轉]BI入門經典
- Kafka入門經典教程Kafka
- BI入門經典 (轉)
- 安卓入門---安卓開發實戰經典1-3章安卓