{"id":911,"date":"2024-11-07T09:23:34","date_gmt":"2024-11-07T01:23:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/911\/"},"modified":"2024-11-07T09:23:34","modified_gmt":"2024-11-07T01:23:34","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-go-%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%a4%e6%96%ad-map-%e4%b8%ad-net-conn-%e7%b1%bb%e5%9e%8b%e5%8f%98%e9%87%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/911\/","title":{"rendered":"\u5982\u4f55\u5728 Go \u8bed\u8a00\u4e2d\u5224\u65ad map \u4e2d net.Conn \u7c7b\u578b\u53d8\u91cf\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-993\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173063109055131.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173063109055131.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173063109055131-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173063109055131-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173063109055131-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"\u5982\u4f55\u5728 Go \u8bed\u8a00\u4e2d\u5224\u65ad map \u4e2d net.Conn \u7c7b\u578b\u53d8\u91cf\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Go \u8bed\u8a00\u4e2d\u5224\u65ad map \u4e2d net.Conn \u7c7b\u578b\u53d8\u91cf\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad map \u4e2d net.conn \u7c7b\u578b\u53d8\u91cf<\/strong><\/p>\n<p>\u5728 go \u8bed\u8a00\u4e2d\uff0cmap \u7684\u503c\u53ef\u4ee5\u5b58\u50a8\u4efb\u610f\u7c7b\u578b\u3002\u5728\u5904\u7406\u5305\u542b net.conn \u7c7b\u578b\u503c\u7684 map \u65f6\uff0c\u6709\u5fc5\u8981\u5224\u65ad\u53d8\u91cf\u7c7b\u578b\u624d\u80fd\u6b63\u786e\u5730\u8bbf\u95ee\u5176\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a []map[string]interface{} \u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u5b83\u5305\u542b\u4e00\u4e2a\u5ba2\u6237\u7aef\u5217\u8868\u3002\u6bcf\u4e2a\u5ba2\u6237\u7aef\u90fd\u6709\u4e00\u4e2a client[&#8220;conn&#8221;] \u5143\u7d20\uff0c\u5176\u4e2d\u5b58\u50a8\u7740 net.conn \u7c7b\u578b\u7684\u53d8\u91cf\u3002\u4e3a\u4e86\u7ed9\u6bcf\u4e2a conn \u53d1\u9001\u6d88\u606f\uff0c\u9700\u8981\u786e\u5b9a\u53d8\u91cf\u7684\u7c7b\u578b\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528\u7c7b\u578b\u65ad\u8a00\u8bed\u6cd5\u6765\u786e\u5b9a\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u7c7b\u578b\u65ad\u8a00\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u8d4b\u503c\u64cd\u4f5c\uff0c\u5b83\u68c0\u67e5\u53d8\u91cf\u662f\u5426\u5c5e\u4e8e\u7279\u5b9a\u7684\u7c7b\u578b\uff0c\u5982\u679c\u662f\uff0c\u5219\u5c06\u53d8\u91cf\u5f3a\u5236\u8f6c\u6362\u4e3a\u8be5\u7c7b\u578b\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>if conn, ok := m[\"conn\"].(*net.TCPConn); ok {\n   \/\/ do something with conn\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u79cd\u8bed\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>m[&#8220;conn&#8221;] \u4ece map \u4e2d\u83b7\u53d6 &#8220;conn&#8221; \u5143\u7d20\u3002<\/li>\n<li>(<em>net.tcpconn) \u5c06 m[&#8220;conn&#8221;] \u7684\u503c\u5f3a\u5236\u8f6c\u6362\u4e3a <\/em>net.tcpconn \u7c7b\u578b\u3002<\/li>\n<li>ok \u662f\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u8f6c\u6362\u662f\u5426\u6210\u529f\u3002\u5982\u679c m[&#8220;conn&#8221;] \u5b9e\u9645\u4e0a\u662f *net.tcpconn \u7c7b\u578b\uff0c\u5219 ok \u4e3a true\u3002<\/li>\n<\/ul>\n<p>\u5728\u7c7b\u578b\u8f6c\u6362\u6210\u529f\u7684\u60c5\u51b5\u4e0b\uff0cconn \u5c06\u5305\u542b\u5ba2\u6237\u7aef\u7684 net.tcpconn \u7c7b\u578b\u53d8\u91cf\u3002\u5c31\u53ef\u4ee5\u4f7f\u7528 conn \u6765\u53d1\u9001\u6d88\u606f\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u8fd9\u79cd\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u65ad\u8a00\uff0c\u5982 <em>net.unixconn\u3001<\/em>net.udpconn \u7b49\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Go \u8bed\u8a00\u4e2d\u5224\u65ad map \u4e2d net.Conn \u7c7b\u578b\u53d8\u91cf\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5224\u65ad map \u4e2d net.conn \u7c7b\u578b\u53d8\u91cf \u5728 go \u8bed\u8a00\u4e2d\uff0cmap \u7684\u503c\u53ef\u4ee5\u5b58\u50a8\u4efb\u610f\u7c7b\u578b\u3002\u5728\u5904\u7406\u5305\u542b net.conn \u7c7b\u578b\u503c\u7684 map \u65f6\uff0c\u6709\u5fc5\u8981\u5224\u65ad\u53d8\u91cf\u7c7b\u578b\u624d\u80fd\u6b63\u786e\u5730\u8bbf\u95ee\u5176\u65b9\u6cd5\u548c\u5c5e\u6027\u3002 \u95ee\u9898\u63cf\u8ff0\uff1a \u5047\u8bbe\u6709\u4e00\u4e2a []map[string]interface{} \u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u5b83\u5305\u542b\u4e00\u4e2a\u5ba2\u6237\u7aef\u5217\u8868\u3002\u6bcf\u4e2a\u5ba2\u6237\u7aef\u90fd\u6709\u4e00\u4e2a client[&#8220;conn&#8221;] \u5143\u7d20\uff0c\u5176\u4e2d\u5b58\u50a8\u7740 net.conn \u7c7b\u578b\u7684\u53d8\u91cf\u3002\u4e3a\u4e86\u7ed9\u6bcf\u4e2a conn \u53d1\u9001\u6d88\u606f\uff0c\u9700\u8981\u786e\u5b9a\u53d8\u91cf\u7684\u7c7b\u578b\u3002 \u89e3\u51b3\u65b9\u6cd5\uff1a \u4f7f\u7528\u7c7b\u578b\u65ad\u8a00\u8bed\u6cd5\u6765\u786e\u5b9a\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u7c7b\u578b\u65ad\u8a00\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u8d4b\u503c\u64cd\u4f5c\uff0c\u5b83\u68c0\u67e5\u53d8\u91cf\u662f\u5426\u5c5e\u4e8e\u7279\u5b9a\u7684\u7c7b\u578b\uff0c\u5982\u679c\u662f\uff0c\u5219\u5c06\u53d8\u91cf\u5f3a\u5236\u8f6c\u6362\u4e3a\u8be5\u7c7b\u578b\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a if conn, ok := m[&#8220;conn&#8221;].(*net.TCPConn); ok { \/\/ do something with conn } \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u79cd\u8bed\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a m[&#8220;conn&#8221;] \u4ece map \u4e2d\u83b7\u53d6 &#8220;conn&#8221; \u5143\u7d20\u3002 (net.tcpconn) \u5c06 m[&#8220;conn&#8221;] \u7684\u503c\u5f3a\u5236\u8f6c\u6362\u4e3a net.tcpconn \u7c7b\u578b\u3002 ok \u662f\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u8f6c\u6362\u662f\u5426\u6210\u529f\u3002\u5982\u679c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-911","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=911"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/911\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}