{"id":3744,"date":"2022-12-30T14:06:03","date_gmt":"2022-12-30T06:06:03","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3744"},"modified":"2022-12-30T14:06:03","modified_gmt":"2022-12-30T06:06:03","slug":"%e5%ad%b8%e7%bf%92go%e8%aa%9e%e8%a8%80%e7%b6%b2%e7%b5%a1%e7%bc%96%e7%a8%8b%e7%9a%84%e5%9f%ba%e7%a4%8e%e7%9f%a5%e8%ad%98","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/go\/%e5%ad%b8%e7%bf%92go%e8%aa%9e%e8%a8%80%e7%b6%b2%e7%b5%a1%e7%bc%96%e7%a8%8b%e7%9a%84%e5%9f%ba%e7%a4%8e%e7%9f%a5%e8%ad%98\/","title":{"rendered":"\u5b78\u7fd2Go\u8a9e\u8a00\u7db2\u7d61\u7f16\u7a0b\u7684\u57fa\u790e\u77e5\u8b58"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Go, \u7db2\u7d61\u7f16\u7a0b\"><\/p>\n<h1>Go \u8a9e\u8a00\u7db2\u7d61\u7f16\u7a0b<\/h1>\n<p>Go \u8a9e\u8a00\u662f\u4e00\u7a2e\u5feb\u901f\u3001\u5b89\u5168\u3001\u53ef\u64f4\u5c55\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5b83\u53ef\u4ee5\u7528\u65bc\u958b\u767c\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\uff0c\u5305\u62ec\u7db2\u7d61\u7f16\u7a0b\u3002Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u7db2\u7d61\u7f16\u7a0b\u6846\u67b6\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u9ad8\u6027\u80fd\u7684\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c07\u4ecb\u7d39 Go \u8a9e\u8a00\u7db2\u7d61\u7f16\u7a0b\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528 Go \u8a9e\u8a00\u4f86\u958b\u767c\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n<h2>Go \u8a9e\u8a00\u7db2\u7d61\u7f16\u7a0b\u6982\u8ff0<\/h2>\n<p>Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u7db2\u7d61\u7f16\u7a0b\u6846\u67b6\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u9ad8\u6027\u80fd\u7684\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u6613\u7528\u7684\u7db2\u7d61\u7f16\u7a0b\u63a5\u53e3\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u9ad8\u6548\u7684\u7db2\u7d61\u7f16\u7a0b\u6846\u67b6\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u9ad8\u6027\u80fd\u7684\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n<h2>Go \u8a9e\u8a00\u7db2\u7d61\u7f16\u7a0b\u793a\u4f8b<\/h2>\n<p>\u4e0b\u9762\u662f\u4e00\u500b\u4f7f\u7528 Go \u8a9e\u8a00\u958b\u767c\u7684\u7db2\u7d61\u7a0b\u5e8f\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: go\">\npackage main\n\nimport (\n    \"fmt\"\n    \"net\"\n)\n\nfunc main() {\n    \/\/ \u76e3\u807d\u7aef\u53e3\n    listener, err := net.Listen(\"tcp\", \":8080\")\n    if err != nil {\n        fmt.Println(\"Error listening:\", err.Error())\n        return\n    }\n    \/\/ \u95dc\u9589\u76e3\u807d\u7aef\u53e3\n    defer listener.Close()\n    fmt.Println(\"Listening on 0.0.0.0:8080\")\n    \/\/ \u7b49\u5f85\u5ba2\u6236\u7aef\u9023\u63a5\n    for {\n        conn, err := listener.Accept()\n        if err != nil {\n            fmt.Println(\"Error accepting: \", err.Error())\n            return\n        }\n        \/\/ \u5efa\u7acb\u65b0\u7684 goroutine \u4f86\u8655\u7406\u5ba2\u6236\u7aef\u8acb\u6c42\n        go handleRequest(conn)\n    }\n}\n\nfunc handleRequest(conn net.Conn) {\n    \/\/ \u95dc\u9589\u9023\u63a5\n    defer conn.Close()\n    \/\/ \u8b80\u53d6\u5ba2\u6236\u7aef\u8acb\u6c42\n    buf := make([]byte, 1024)\n    reqLen, err := conn.Read(buf)\n    if err != nil {\n        fmt.Println(\"Error reading:\", err.Error())\n    }\n    \/\/ \u56de\u61c9\u5ba2\u6236\u7aef\u8acb\u6c42\n    conn.Write(buf[:reqLen])\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5e8f\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7db2\u7d61\u7a0b\u5e8f\uff0c\u5b83\u53ef\u4ee5\u76e3\u807d\u4e00\u500b\u7aef\u53e3\uff0c\u4e26\u5728\u6536\u5230\u5ba2\u6236\u7aef\u8acb\u6c42\u6642\u56de\u61c9\u5ba2\u6236\u7aef\u8acb\u6c42\u3002<\/p>\n<p><!--more--><\/p>\n<h2>Go \u8a9e\u8a00\u7db2\u7d61\u7f16\u7a0b\u7684\u512a\u52e2<\/h2>\n<p>Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u7db2\u7d61\u7f16\u7a0b\u6846\u67b6\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u9ad8\u6027\u80fd\u7684\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u6613\u7528\u7684\u7db2\u7d61\u7f16\u7a0b\u63a5\u53e3\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u9ad8\u6548\u7684\u7db2\u7d61\u7f16\u7a0b\u6846\u67b6\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u9ad8\u6027\u80fd\u7684\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u5b89\u5168\u53ef\u9760\u7684\u7db2\u7d61\u7f16\u7a0b\u6846\u67b6\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u5b89\u5168\u53ef\u9760\u7684\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u7db2\u7d61\u7f16\u7a0b\u6846\u67b6\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u9ad8\u6027\u80fd\u3001\u5b89\u5168\u53ef\u9760\u7684\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u6613\u7528\u7684\u7db2\u7d61\u7f16\u7a0b\u63a5\u53e3\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u9ad8\u6548\u7684\u7db2\u7d61\u7f16\u7a0b\u6846\u67b6\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u9ad8\u6027\u80fd\u7684\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u5b89\u5168\u53ef\u9760\u7684\u7db2\u7d61\u7f16\u7a0b\u6846\u67b6\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u5b89\u5168\u53ef\u9760\u7684\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002\u56e0\u6b64\uff0cGo \u8a9e\u8a00\u662f\u4e00\u500b\u7406\u60f3\u7684\u9078\u64c7\uff0c\u53ef\u4ee5\u7528\u65bc\u958b\u767c\u5404\u7a2e\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aGo\u8a9e\u8a00\u7db2\u7d61\u7f16\u7a0b\u662f\u4e00\u9580\u6280\u8853\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u69cb\u5efa\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c07\u4ecb\u7d39Go\u8a9e\u8a00\u7db2\u7d61\u7f16\u7a0b\u7684\u57fa\u790e\u77e5\u8b58\uff0c\u5305\u62ec\u7db2\u7d61\u5354\u8b70\u3001\u7db2\u7d61\u67b6\u69cb\u3001\u7db2\u7d61\u5b89\u5168\u7b49\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[171,172],"tags":[169,170],"class_list":["post-3744","post","type-post","status-publish","format-standard","hentry","category-go","category-golang","tag-go","tag-golang"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Yo","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3744"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3744\/revisions"}],"predecessor-version":[{"id":3745,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3744\/revisions\/3745"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}