{"id":184329,"date":"2024-05-08T22:06:04","date_gmt":"2024-05-08T14:06:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/184329.html"},"modified":"2024-05-08T22:06:07","modified_gmt":"2024-05-08T14:06:07","slug":"%e6%9c%89%e8%b0%b7%e6%ad%8c%e9%9d%a2%e8%af%95%e9%a2%98%e5%8f%8a%e7%ad%94%e6%a1%88%e5%90%97-%e6%9c%80%e5%a5%bd%e6%98%afgo%e8%af%ad%e8%a8%80%e7%bc%96%e7%a8%8b%e7%9a%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/184329.html","title":{"rendered":"\u6709\u8c37\u6b4c\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u5417 \u6700\u597d\u662fgo\u8bed\u8a00\u7f16\u7a0b\u7684"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27075252\/2057840b-89a8-48e4-9be8-7907630833db.webp\" alt=\"\u6709\u8c37\u6b4c\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u5417 \u6700\u597d\u662fgo\u8bed\u8a00\u7f16\u7a0b\u7684\" \/><\/p>\n<p><p>\u6709\u8c37\u6b4c\u9762\u8bd5\u9898\u53ca\u5176\u7b54\u6848\u662f\u5b58\u5728\u7684\uff0c\u9762\u8bd5\u9898\u76ee\u80cc\u540e\u7684\u76ee\u7684\u5728\u4e8e\u8bc4\u4f30\u6c42\u804c\u8005\u5728\u7f16\u7a0b\u3001\u7b97\u6cd5\u8bbe\u8ba1\u3001\u6570\u636e\u7ed3\u6784\u7406\u89e3\u3001\u89e3\u51b3\u95ee\u9898\u80fd\u529b\u4ee5\u53ca\u7f16\u7801\u98ce\u683c\u7684\u6c34\u5e73\u3002\u5bf9\u4e8e\u4f7f\u7528Go\u8bed\u8a00\u7f16\u7a0b\u7684\u9762\u8bd5\uff0c<strong>\u95ee\u9898\u53ef\u80fd\u5305\u62ec\u6570\u636e\u7ed3\u6784\u5904\u7406\u3001\u5e38\u89c1\u7b97\u6cd5\u5b9e\u73b0\u3001\u5e76\u53d1\u7f16\u7a0b\u548c\u6807\u51c6\u5e93\u7684\u5e94\u7528<\/strong>\u3002\u5728Go\u8bed\u8a00\u7684\u7f16\u7a0b\u9762\u8bd5\u4e2d\uff0c\u4e00\u4e2a\u5178\u578b\u7684\u95ee\u9898\u53ef\u4ee5\u662f\u5982\u4f55\u5b9e\u73b0\u5e76\u53d1\u6a21\u5f0f\u6216\u600e\u6837\u5904\u7406\u5207\u7247(slice)\u548c\u6620\u5c04(map)\u7b49\u6570\u636e\u7ed3\u6784\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5177\u4f53\u6df1\u5165\u8fd9\u4e9b\u9886\u57df\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Go\u8bed\u8a00\u7f16\u7a0b\u4e2d\uff0c\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u662f\u8861\u91cf\u5f00\u53d1\u4eba\u5458\u6c34\u5e73\u7684\u5173\u952e\u6307\u6807\u3002\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u5982\u4f55\u5728Go\u4e2d\u5b9e\u73b0\u7279\u5b9a\u6570\u636e\u7ed3\u6784\uff08\u5982\u94fe\u8868\u3001\u6811\u3001\u5806\u7b49\uff09\u7684\u95ee\u9898\uff0c\u6216\u8005\u88ab\u8981\u6c42\u5c55\u793a\u6392\u5e8f\u548c\u641c\u7d22\u7b97\u6cd5\u7684\u77e5\u8bc6\u3002<\/p>\n<\/p>\n<p><h4>\u94fe\u8868\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u94fe\u8868\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9762\u8bd5\u5b98\u53ef\u80fd\u4f1a\u8981\u6c42\u4f60\u5b9e\u73b0\u5355\u5411\u6216\u53cc\u5411\u94fe\u8868\u7684\u63d2\u5165\u3001\u5220\u9664\u3001\u53cd\u8f6c\u7b49\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c<strong>\u7f16\u5199\u4e00\u4e2a\u53cd\u8f6c\u5355\u94fe\u8868\u7684Go\u51fd\u6570<\/strong>\u5c31\u662f\u4e00\u4e2a\u5178\u578b\u7684\u9762\u8bd5\u9898\u76ee\u3002<\/p>\n<\/p>\n<p><h4>\u6392\u5e8f\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u6392\u5e8f\u662f\u89e3\u51b3\u95ee\u9898\u7684\u57fa\u7840\uff0c<strong>\u5feb\u901f\u6392\u5e8f\u548c\u5f52\u5e76\u6392\u5e8f<\/strong>\u662f\u9762\u8bd5\u4e2d\u7ecf\u5e38\u51fa\u73b0\u7684\u8bdd\u9898\u3002\u7406\u89e3\u5b83\u4eec\u7684\u539f\u7406\u548c\u5728Go\u8bed\u8a00\u4e2d\u5982\u4f55\u6709\u6548\u5b9e\u73b0\u8fd9\u4e9b\u7b97\u6cd5\u662f\u89e3\u7b54\u6b64\u7c7b\u95ee\u9898\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5e76\u53d1\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>Go\u8bed\u8a00\u4ee5\u539f\u751f\u652f\u6301\u5e76\u53d1\u800c\u8457\u79f0\uff0c\u56e0\u6b64\u5728\u9762\u8bd5\u4e2d\u53ef\u80fd\u4f1a\u95ee\u5230\u6709\u5173<code>goroutine<\/code>\u548c<code>channel<\/code>\u7684\u95ee\u9898\u3002<strong>\u7406\u89e3\u5982\u4f55\u4f7f\u7528goroutine\u8fdb\u884c\u5e76\u884c\u5904\u7406\uff0c\u4ee5\u53ca\u5982\u4f55\u901a\u8fc7channel\u8fdb\u884c\u534f\u7a0b\u95f4\u7684\u901a\u4fe1<\/strong>\uff0c\u8fd9\u4e9b\u90fd\u662f\u4f1a\u88ab\u8003\u5230\u7684\u70b9\u3002<\/p>\n<\/p>\n<p><h4>Goroutines<\/h4>\n<\/p>\n<p><p>Goroutines\u662fGo\u8bed\u8a00\u5e76\u53d1\u6a21\u578b\u7684\u6838\u5fc3\uff0c\u9762\u8bd5\u53ef\u80fd\u4f1a\u6d89\u53ca\u7f16\u5199\u4f7f\u7528goroutines\u7684\u4ee3\u7801\u7247\u6bb5\uff0c\u4f8b\u5982\u5b9e\u73b0\u4e00\u4e2a\u5e76\u53d1\u7f51\u7edc\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><h4>Channels<\/h4>\n<\/p>\n<p><p>Channel\u662fgoroutines\u4e4b\u95f4\u7684\u901a\u4fe1\u673a\u5236\u3002\u4f7f\u7528channels\u53ef\u4ee5\u540c\u6b65\u6216\u4f20\u9012\u6570\u636e\u3002\u9762\u8bd5\u4e2d\u53ef\u80fd\u4f1a\u95ee\u5230\u5982\u4f55\u521b\u5efabuffered\u548cunbuffered channels\uff0c\u6216\u8005\u5982\u4f55\u5229\u7528select\u8bed\u53e5\u5904\u7406\u591a\u4e2achannels\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6807\u51c6\u5e93\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>Go\u8bed\u8a00\u6709\u4e00\u4e2a\u4e30\u5bcc\u7684\u6807\u51c6\u5e93\uff0c\u5b83\u5305\u62ec\u5404\u79cd\u5b9e\u7528\u7684\u5de5\u5177\u548c\u51fd\u6570\u3002<strong>\u719f\u6089\u6807\u51c6\u5e93\u4e2d\u7684\u5305\u548c\u5b83\u4eec\u7684\u4f7f\u7528<\/strong>\u662f\u8fdb\u884c\u9ad8\u6548Go\u8bed\u8a00\u7f16\u7a0b\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><h4>\u5b57\u7b26\u4e32\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u6807\u51c6\u5e93\u4e2d\u7684<code>strings<\/code>\u5305\u542b\u6709\u5e7f\u6cdb\u7684\u5b57\u7b26\u4e32\u5904\u7406\u529f\u80fd\u3002\u9762\u8bd5\u53ef\u80fd\u4f1a\u5305\u62ec\u5982\u4f55\u4f7f\u7528\u8fd9\u4e2a\u5305\u6765\u5206\u5272\u3001\u8fde\u63a5\u3001\u641c\u7d22\u6216\u4fee\u6539\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>\u6587\u4ef6\u548cIO\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p><code>io<\/code>\u548c<code>os<\/code>\u5305\u63d0\u4f9b\u4e86\u6587\u4ef6\u64cd\u4f5c\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u9884\u671f\u7684\u9762\u8bd5\u95ee\u9898\u53ef\u80fd\u4f1a\u8ba9\u4f60\u8bfb\u53d6\u3001\u5199\u5165\u3001\u521b\u5efa\u6216\u5220\u9664\u6587\u4ef6\uff0c\u4ee5\u53ca\u5904\u7406\u5176\u4ed6IO\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u7f16\u7a0b\u6311\u6218<\/h3>\n<\/p>\n<p><p>\u5728\u9762\u8bd5\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u5177\u4f53\u7684\u7f16\u7a0b\u95ee\u9898\uff0c\u8981\u6c42\u4f60\u73b0\u573a\u7f16\u7801\u3002\u8fd9\u4e9b\u95ee\u9898\u6d4b\u8bd5\u4f60\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u7684\u80fd\u529b\u548c\u7f16\u7801\u98ce\u683c\u3002<\/p>\n<\/p>\n<p><h4>\u5b9e\u73b0RESTful API<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684RESTful API\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u7f16\u7a0b\u6311\u6218\uff0c\u9762\u8bd5\u5b98\u5e0c\u671b\u770b\u5230\u4f60\u5982\u4f55\u4f7f\u7528HTTP\u5305\u521b\u5efaWeb\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><h4>JSON\u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5904\u7406JSON\u6570\u636e\u5728Web\u7f16\u7a0b\u4e2d\u662f\u975e\u5e38\u5e38\u89c1\u7684\u3002\u4f60\u53ef\u80fd\u9700\u8981\u7f16\u5199Go\u4ee3\u7801\u6765\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316JSON\uff0c\u5e76\u4e14\u80fd\u591f\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p>\u5728\u51c6\u5907Go\u8bed\u8a00\u7684\u8c37\u6b4c\u9762\u8bd5\u65f6\uff0c\u9664\u4e86\u6df1\u5165\u5b66\u4e60\u4e0a\u8ff0\u5185\u5bb9\u5916\uff0c<strong>\u8fd8\u8981\u5b9e\u8df5\u7f16\u5199\u5e72\u51c0\u3001\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801<\/strong>\uff0c\u4ee5\u53ca\u719f\u7ec3\u638c\u63e1\u5e38\u89c1\u7f16\u7a0b\u95ee\u9898\u548c\u601d\u7ef4\u65b9\u5f0f\u3002\u5b9e\u9645\u4e0a\uff0c\u5728\u51c6\u5907\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u4e5f\u5e94\u8be5\u7ec3\u4e60\u5728\u538b\u529b\u4e0b\u7f16\u7801\uff0c\u7ec3\u4e60\u5982\u4f55\u6e05\u6670\u5730\u89e3\u91ca\u4f60\u7684\u601d\u8def\u548c\u51b3\u7b56\uff0c\u56e0\u4e3a\u8fd9\u662f\u9762\u8bd5\u8fc7\u7a0b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4ec0\u4e48\u662f\u8c37\u6b4c\u9762\u8bd5\u9898\uff1f<\/strong><br \/>\n\u8c37\u6b4c\u9762\u8bd5\u9898\u662f\u8c37\u6b4c\u516c\u53f8\u5728\u62db\u8058\u8fc7\u7a0b\u4e2d\u63d0\u51fa\u7684\u4e00\u7cfb\u5217\u95ee\u9898\uff0c\u65e8\u5728\u6d4b\u8bd5\u5e94\u8058\u8005\u7684\u7f16\u7a0b\u80fd\u529b\u3001\u89e3\u51b3\u95ee\u9898\u7684\u80fd\u529b\u548c\u521b\u9020\u6027\u601d\u7ef4\u3002\u8fd9\u4e9b\u9898\u76ee\u591a\u6837\u5316\uff0c\u8986\u76d6\u4e86\u5404\u4e2a\u7f16\u7a0b\u8bed\u8a00\u548c\u6280\u672f\u9886\u57df\u3002<\/p>\n<p><strong>\u8c37\u6b4c\u9762\u8bd5\u9898\u4e2d\u662f\u5426\u6709\u6d89\u53caGo\u8bed\u8a00\u7f16\u7a0b\u7684\u9898\u76ee\uff1f<\/strong><br \/>\n\u662f\u7684\uff0c\u8c37\u6b4c\u9762\u8bd5\u4e2d\u7ecf\u5e38\u4f1a\u5305\u542b\u4e0eGo\u8bed\u8a00\u76f8\u5173\u7684\u7f16\u7a0b\u9898\u76ee\u3002Go\u8bed\u8a00\u662f\u4e00\u79cd\u5f00\u6e90\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u7531\u8c37\u6b4c\u5f00\u53d1\u5e76\u5e7f\u6cdb\u7528\u4e8e\u6784\u5efa\u53ef\u4f38\u7f29\u7684\u7f51\u7edc\u670d\u52a1\u548c\u5206\u5e03\u5f0f\u7cfb\u7edf\u3002\u56e0\u6b64\uff0c\u8c37\u6b4c\u5728\u7f16\u7a0b\u9762\u8bd5\u4e2d\u901a\u5e38\u4f1a\u6d89\u53caGo\u8bed\u8a00\u7684\u76f8\u5173\u9898\u76ee\uff0c\u4ee5\u6d4b\u8bd5\u5e94\u8058\u8005\u5728\u8be5\u8bed\u8a00\u4e0a\u7684\u638c\u63e1\u7a0b\u5ea6\u3002<\/p>\n<p><strong>\u5982\u4f55\u51c6\u5907\u8c37\u6b4c\u9762\u8bd5\u4e2d\u7684Go\u8bed\u8a00\u7f16\u7a0b\u9898\u76ee\uff1f<\/strong><br \/>\n\u51c6\u5907\u8c37\u6b4c\u9762\u8bd5\u4e2d\u7684Go\u8bed\u8a00\u7f16\u7a0b\u9898\u76ee\uff0c\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u5165\u624b\uff1a<\/p>\n<ol>\n<li>\u5b66\u4e60Go\u8bed\u8a00\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5305\u62ec\u8bed\u6cd5\u3001\u6570\u636e\u7c7b\u578b\u3001\u63a7\u5236\u6d41\u3001\u51fd\u6570\u3001\u7ed3\u6784\u4f53\u7b49\uff1b<\/li>\n<li>\u719f\u6089Go\u8bed\u8a00\u7684\u6807\u51c6\u5e93\u548c\u5e38\u7528\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\uff1b<\/li>\n<li>\u9488\u5bf9Go\u8bed\u8a00\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u9898\u76ee\u8fdb\u884c\u7ec3\u4e60\uff0c\u63d0\u9ad8\u89e3\u51b3\u8fd9\u7c7b\u95ee\u9898\u7684\u80fd\u529b\uff1b<\/li>\n<li>\u5b66\u4e60\u548c\u4e86\u89e3\u8c37\u6b4c\u7684\u7f16\u7a0b\u51c6\u5219\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u8fd9\u5bf9\u4e8e\u8c37\u6b4c\u9762\u8bd5\u5c24\u4e3a\u91cd\u8981\uff1b<\/li>\n<li>\u591a\u53c2\u52a0\u7f16\u7a0b\u6311\u6218\u548c\u7ade\u8d5b\uff0c\u953b\u70bc\u81ea\u5df1\u7684\u7f16\u7a0b\u80fd\u529b\u548c\u89e3\u51b3\u95ee\u9898\u7684\u601d\u7ef4\u65b9\u5f0f\u3002<\/li>\n<\/ol>\n<p>\u603b\u7684\u6765\u8bf4\uff0c\u901a\u8fc7\u6df1\u5165\u5b66\u4e60Go\u8bed\u8a00\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u4ee5\u53ca\u8fdb\u884c\u5927\u91cf\u5b9e\u8df5\u548c\u7ec3\u4e60\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u4e3a\u8c37\u6b4c\u9762\u8bd5\u4e2d\u7684Go\u8bed\u8a00\u7f16\u7a0b\u9898\u76ee\u505a\u597d\u51c6\u5907\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6709\u8c37\u6b4c\u9762\u8bd5\u9898\u53ca\u5176\u7b54\u6848\u662f\u5b58\u5728\u7684\uff0c\u9762\u8bd5\u9898\u76ee\u80cc\u540e\u7684\u76ee\u7684\u5728\u4e8e\u8bc4\u4f30\u6c42\u804c\u8005\u5728\u7f16\u7a0b\u3001\u7b97\u6cd5\u8bbe\u8ba1\u3001\u6570\u636e\u7ed3\u6784\u7406\u89e3\u3001\u89e3\u51b3\u95ee\u9898\u80fd\u529b\u4ee5\u53ca [&hellip;]","protected":false},"author":3,"featured_media":184333,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/184329"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=184329"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/184329\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/184333"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=184329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=184329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=184329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}