{"id":4153,"date":"2018-11-01T08:26:12","date_gmt":"2018-11-01T00:26:12","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4153"},"modified":"2018-11-01T08:26:12","modified_gmt":"2018-11-01T00:26:12","slug":"go%e8%af%ad%e8%a8%80%e5%ad%a6%e4%b9%a012-%e5%a6%82%e4%bd%95%e6%90%ad%e5%bb%bahttp%e6%9c%8d%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4153.html","title":{"rendered":"Go\u8bed\u8a00\u5b66\u4e60#12-\u5982\u4f55\u642d\u5efaHTTP\u670d\u52a1"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u5b66\u4e60\u6574\u7406\u4e00\u4e0b\u5982\u4f55\u7528Go \u8bed\u8a00\u5b9e\u73b0\u7b80\u5355\u7684HTTP\u670d\u52a1\uff0c\u4ee5\u4fbf\u540e\u7eed\u53c2\u8003\u548c\u4f7f\u7528\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6><strong>1\u3001\u4e00\u4e2a\u8d85\u7b80\u5355\u7684HTTP\u670d\u52a1\u6837\u4f8b<\/strong><\/h6>\n<pre class=\"lang:default decode:true \">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"log\"\r\n    \"net\/http\"\r\n)\r\n\r\n\/\/ w \u8868\u793aresponse\u5bf9\u8c61\uff0c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u7684\u5185\u5bb9\u90fd\u5728\u5bf9\u8c61w\u91cc\u5904\u7406\r\n\/\/ r \u8868\u793a\u5ba2\u6237\u7aef\u8bf7\u6c42\u5bf9\u8c61\uff0c\u5305\u542b\u4e86\u8bf7\u6c42\u5934\uff0c\u8bf7\u6c42\u53c2\u6570\u7b49\u7b49\r\nfunc index(w http.ResponseWriter, r *http.Request) {\r\n    \/\/ \u5f80w\u91cc\u5199\u5165\u5185\u5bb9\uff0c\u5c31\u4f1a\u5728\u6d4f\u89c8\u5668\u91cc\u8f93\u51fa\r\n    fmt.Fprintf(w, \"Hello golang http!\")\r\n}\r\n\r\nfunc main() {\r\n    \/\/ \u8bbe\u7f6e\u8def\u7531\uff0c\u5982\u679c\u8bbf\u95ee\/\uff0c\u5219\u8c03\u7528index\u65b9\u6cd5\r\n    http.HandleFunc(\"\/\", index)\r\n\r\n    \/\/ \u542f\u52a8web\u670d\u52a1\uff0c\u76d1\u542c9090\u7aef\u53e3\r\n    err := http.ListenAndServe(\":9090\", nil)\r\n    if err != nil {\r\n        log.Fatal(\"ListenAndServe:9090\", err)\r\n    }\r\n}<\/pre>\n<p>\u4fdd\u5b58\u4e3a go_http.go \u7136\u540e\u7528\u547d\u4ee4 <code>go run go_http.go<\/code>\u542f\u52a8HTTP\u670d\u52a1\u3002<\/p>\n<h6><span style=\"color: #ff0000;\"><strong>2\u3001\u63d0\u4f9b\u9759\u6001\u6587\u4ef6Web\u670d\u52a1<\/strong><\/span><\/h6>\n<pre class=\"lang:default decode:true\">package main\r\n\r\nimport (\r\n    \"log\"\r\n    \"net\/http\"\r\n)\r\n\r\nfunc main() {\r\n    \/\/ \u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\uff0c\u5728 \/ \u4e0b\u63d0\u4f9b\u6587\u4ef6\u670d\u52a1\r\n    \/\/ log.Fatal(http.ListenAndServe(\":9090\", http.FileServer(http.Dir(\"\/usr\/share\/doc\"))))\r\n\r\n    \/\/ \u6216\r\n\r\n    \/\/ \u5728\u6307\u5b9a URL \u4e0b\u63d0\u4f9b\u6587\u4ef6\u670d\u52a1\r\n    \/\/ \u5c06\u672c\u5730\u78c1\u76d8\u4e2d\u7684\u76ee\u5f55(\/usr\/share\/doc)\u6302\u8f7d\u5728URL(\/doc\/)\u4e0b\u9762\r\n    \/\/ \u53ef\u4ee5\u4f7f\u7528 StripPrefix() \u65b9\u6cd5\u5728 FileServer \u770b\u5230\u8bf7\u6c42\u4e4b\u524d\u4fee\u6539\u8bf7\u6c42URL\u7684\u8def\u5f84\r\n    http.Handle(\"\/doc\/\", http.StripPrefix(\"\/doc\/\", http.FileServer(http.Dir(\"\/usr\/share\/doc\"))))\r\n\r\n    \/\/ \u542f\u52a8web\u670d\u52a1\uff0c\u76d1\u542c9090\u7aef\u53e3\r\n    err := http.ListenAndServe(\":9090\", nil)\r\n    if err != nil {\r\n        log.Fatal(\"ListenAndServe: \", err)\r\n    }\r\n}<\/pre>\n<h6><span style=\"color: #ff0000;\"><strong>3\u3001\u8bfb\u53d6HTTP\u7684header\/form\/body\u5185\u5bb9\u7684\u6837\u4f8b<\/strong><\/span><\/h6>\n<pre class=\"lang:default decode:true \">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"io\/ioutil\"\r\n    \"log\"\r\n    \"net\/http\"\r\n)\r\n\r\n\/\/ w \u8868\u793aresponse\u5bf9\u8c61\uff0c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u7684\u5185\u5bb9\u90fd\u5728\u5bf9\u8c61w\u91cc\u5904\u7406\r\n\/\/ r \u8868\u793a\u5ba2\u6237\u7aef\u8bf7\u6c42\u5bf9\u8c61\uff0c\u5305\u542b\u4e86\u8bf7\u6c42\u5934\uff0c\u8bf7\u6c42\u53c2\u6570\u7b49\u7b49\r\nfunc index(w http.ResponseWriter, r *http.Request) {\r\n    fmt.Printf(\"Request.Method: %v\\n\", r.Method)\r\n    \r\n    fmt.Println(\"Request.Header:\")\r\n    for name, value := range r.Header {\r\n        fmt.Printf(\"%v: %v(%v)\", name, value, len(value))\r\n        if len(value) &gt; 1 {\r\n            for idx, value := range value {\r\n                fmt.Printf(\"\\tidx: %v, value: %v\\n\", idx, value)\r\n            }\r\n        } else {\r\n            fmt.Println()\r\n        }\r\n    }\r\n    fmt.Println()\r\n\r\n    r.ParseForm()\r\n    fmt.Println(\"Request.Form:\")\r\n    for name, value := range r.Form {\r\n        fmt.Printf(\"%v: %v(%v)\", name, value, len(value))\r\n        if len(value) &gt; 1 {\r\n            for idx, value := range value {\r\n                fmt.Printf(\"\\tidx: %v, value: %v\\n\", idx, value)\r\n            }\r\n        } else {\r\n            fmt.Println()\r\n        }\r\n    }\r\n    fmt.Println()\r\n\r\n    s, _ := ioutil.ReadAll(r.Body) \/\/\u628a body \u5185\u5bb9\u8bfb\u5165\u5b57\u7b26\u4e32 s\r\n    fmt.Printf(\"Request.Body: %v\\n\", string(s))\r\n    fmt.Println()\r\n\r\n    \/\/ \u5f80w\u91cc\u5199\u5165\u5185\u5bb9\uff0c\u5c31\u4f1a\u5728\u6d4f\u89c8\u5668\u91cc\u8f93\u51fa\r\n    fmt.Fprintf(w, \"Hello golang http!\")\r\n}\r\n\r\nfunc main() {\r\n    \/\/ \u7b80\u5355\u7684\u9759\u6001Web\u670d\u52a1\u5668\r\n    \/\/ log.Fatal(http.ListenAndServe(\":9090\", http.FileServer(http.Dir(\"\/usr\/share\/doc\"))))\r\n\r\n    \/\/ \/\/ \u8bbe\u7f6e\u8def\u7531\uff0c\u5982\u679c\u8bbf\u95ee\/\uff0c\u5219\u8c03\u7528index\u65b9\u6cd5\r\n    http.HandleFunc(\"\/\", index)\r\n\r\n    \/\/ \u5c06\u672c\u5730\u78c1\u76d8\u4e2d\u7684\u76ee\u5f55(\/tmp)\u6302\u8f7d\u5728URL(\/tmpfiles\/)\u4e0b\u9762\r\n    \/\/ \u53ef\u4ee5\u4f7f\u7528 StripPrefix() \u65b9\u6cd5\u5728 FileServer \u770b\u5230\u4e4b\u524d\u4fee\u6539\u8bf7\u6c42URL\u7684\u8def\u5f84\r\n    http.Handle(\"\/tmpfiles\/\", http.StripPrefix(\"\/tmpfiles\/\", http.FileServer(http.Dir(\"\/tmp\"))))\r\n\r\n    \/\/ \u542f\u52a8web\u670d\u52a1\uff0c\u76d1\u542c9090\u7aef\u53e3\r\n    err := http.ListenAndServe(\":9090\", nil)\r\n    if err != nil {\r\n        log.Fatal(\"ListenAndServe: \", err)\r\n    }\r\n}<\/pre>\n<p>\u4e3b\u8981\u8d77\u6f14\u793a\u4f5c\u7528\uff0c\u5982\u679c\u60f3\u77e5\u9053\u66f4\u591ahttp.Request\u548chttp.ResponseWriter\u6709\u54ea\u4e9b\u5b57\u6bb5\uff0c\u53ef\u4ee5\u8fbe\u5230\u4ec0\u4e48\u529f\u80fd\uff0c\u53ef\u4ee5\u53bbgodoc\u91cc\u9762\u770b\uff0c\u975e\u5e38\u65b9\u4fbf\u548c\u7b80\u5355\u3002<\/p>\n<pre class=\"lang:default decode:true \">type Request struct {\r\n    Method string\r\n\r\n    URL *url.URL\r\n\r\n    Proto      string \/\/ \"HTTP\/1.0\"\r\n    ProtoMajor int    \/\/ 1\r\n    ProtoMinor int    \/\/ 0\r\n\r\n    \/\/\tHeader = map[string][]string{\r\n    \/\/\t\t\"Accept-Encoding\": {\"gzip, deflate\"},\r\n    \/\/\t\t\"Accept-Language\": {\"en-us\"},\r\n    \/\/\t\t\"Foo\": {\"Bar\", \"two\"},\r\n    \/\/\t}\r\n    Header Header\r\n\r\n    \/\/ Body is the request's body.\r\n    \/\/\r\n    \/\/ For client requests a nil body means the request has no\r\n    \/\/ body, such as a GET request. The HTTP Client's Transport\r\n    \/\/ is responsible for calling the Close method.\r\n    \/\/\r\n    \/\/ For server requests the Request Body is always non-nil\r\n    \/\/ but will return EOF immediately when no body is present.\r\n    \/\/ The Server will close the request body. The ServeHTTP\r\n    \/\/ Handler does not need to.\r\n    Body io.ReadCloser\r\n\r\n    \/\/ For server requests it is unused.\r\n    GetBody func() (io.ReadCloser, error)\r\n\r\n    ContentLength int64\r\n\r\n    TransferEncoding []string\r\n\r\n    Close bool\r\n\r\n    Host string\r\n\r\n    \/\/ Form contains the parsed form data, including both the URL\r\n    \/\/ field's query parameters and the POST or PUT form data.\r\n    \/\/ This field is only available after ParseForm is called.\r\n    \/\/ The HTTP client ignores Form and uses Body instead.\r\n    Form url.Values\r\n\r\n    \/\/ PostForm contains the parsed form data from POST, PATCH,\r\n    \/\/ or PUT body parameters.\r\n    \/\/ This field is only available after ParseForm is called.\r\n    \/\/ The HTTP client ignores PostForm and uses Body instead.\r\n    PostForm url.Values\r\n\r\n    \/\/ MultipartForm is the parsed multipart form, including file uploads.\r\n    \/\/ This field is only available after ParseMultipartForm is called.\r\n    \/\/ The HTTP client ignores MultipartForm and uses Body instead.\r\n    MultipartForm *multipart.Form\r\n\r\n    \/\/ Few HTTP clients, servers, or proxies support HTTP trailers.\r\n    Trailer Header\r\n\r\n    \/\/ This field is ignored by the HTTP client.\r\n    RemoteAddr string\r\n\r\n    RequestURI string\r\n\r\n    TLS *tls.ConnectionState\r\n\r\n    Cancel &lt;-chan struct{}\r\n\r\n    Response *Response\r\n    \/\/ contains filtered or unexported fields\r\n}\r\n<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true \">\/\/ url.Values\r\ntype Values map[string][]string\r\n\r\n\/\/ url.URL\r\ntype URL struct {\r\n    Scheme     string\r\n    Opaque     string    \/\/ encoded opaque data\r\n    User       *Userinfo \/\/ username and password information\r\n    Host       string    \/\/ host or host:port\r\n    Path       string\r\n    RawPath    string \/\/ encoded path hint (Go 1.5 and later only; see EscapedPath method)\r\n    ForceQuery bool   \/\/ append a query ('?') even if RawQuery is empty\r\n    RawQuery   string \/\/ encoded query values, without '?'\r\n    Fragment   string \/\/ fragment for references, without '#'\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/www.01happy.com\/golang-simple-http-server\/\">golang\u542f\u52a8\u4e00\u4e2a\u7b80\u5355\u7684http\u670d\u52a1<\/a><\/li>\n<li><a href=\"http:\/\/www.01happy.com\/supervisor-golang-daemon\/\">supervisor\u8fd0\u884cgolang\u5b88\u62a4\u8fdb\u7a0b<\/a><\/li>\n<li><a href=\"https:\/\/medium.com\/doing-things-right\/pretty-printing-http-requests-in-golang-a918d5aaa000\">Pretty Printing HTTP Requests in Golang<\/a><\/li>\n<li><a href=\"https:\/\/golang.org\/pkg\/net\/http\/\">https:\/\/golang.org\/pkg\/net\/http\/<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5b66\u4e60\u6574\u7406\u4e00\u4e0b\u5982\u4f55\u7528Go \u8bed\u8a00\u5b9e\u73b0\u7b80\u5355\u7684HTTP\u670d\u52a1\uff0c\u4ee5\u4fbf\u540e\u7eed\u53c2\u8003\u548c\u4f7f\u7528\u3002 \u6b63\u6587\uff1a \u53c2\u8003 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[564,1246],"class_list":["post-4153","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-golang","tag-net-http"],"views":3465,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4153"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4153\/revisions"}],"predecessor-version":[{"id":4154,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4153\/revisions\/4154"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}