{"id":6577,"date":"2024-09-20T19:10:48","date_gmt":"2024-09-20T11:10:48","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6577"},"modified":"2024-09-20T19:10:48","modified_gmt":"2024-09-20T11:10:48","slug":"%e5%bf%85%e7%9f%a5%e5%bf%85%e4%bc%9a-go-%e7%9a%84%e5%85%b3%e9%94%ae%e7%89%b9%e6%80%a7%e5%92%8c%e9%87%8d%e7%82%b9%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6577","title":{"rendered":"\u5fc5\u77e5\u5fc5\u4f1a &#8211; Go \u7684\u5173\u952e\u7279\u6027\u548c\u91cd\u70b9\u6a21\u5757"},"content":{"rendered":"<p>\u8981\u80dc\u4efb Go \u8bed\u8a00\u7684\u8d44\u6df1\u540e\u7aef\u5f00\u53d1\u5de5\u7a0b\u5e08\u804c\u4f4d\uff0c\u5173\u952e\u7279\u6027\u548c\u6a21\u5757\u7684\u5b66\u4e60\u5e94\u8be5\u805a\u7126\u4e8e\u4ee5\u4e0b\u9886\u57df\uff1a<\/p>\n<h3>1. <strong>Go \u57fa\u7840\u7279\u6027<\/strong><\/h3>\n<ul>\n<li><strong>Goroutines \u548c\u5e76\u53d1<\/strong>\uff1aGo \u7684\u5e76\u53d1\u6a21\u578b\u662f\u57fa\u4e8e\u8f7b\u91cf\u7ea7\u7ebf\u7a0b Goroutines \u7684\uff0c\u719f\u7ec3\u638c\u63e1 Goroutines \u7684\u521b\u5efa\u3001\u7ba1\u7406\u548c\u540c\u6b65\uff08\u5982 <code>sync.WaitGroup<\/code>\u3001<code>sync.Mutex<\/code>\u3001<code>sync.Cond<\/code>\uff09\u81f3\u5173\u91cd\u8981\u3002\u8fd8\u9700\u8981\u4e86\u89e3 <code>select<\/code> \u548c <code>channels<\/code> \u7684\u673a\u5236\uff0c\u7528\u6765\u7ba1\u7406 Goroutines \u95f4\u7684\u901a\u4fe1\u3002<\/li>\n<li><strong>\u5185\u5b58\u7ba1\u7406\u548c\u5783\u573e\u56de\u6536<\/strong>\uff1a\u7406\u89e3 Go \u7684\u5185\u5b58\u5206\u914d\u673a\u5236\uff0c\u5305\u62ec\u9003\u9038\u5206\u6790\uff08escape analysis\uff09\u3001\u5806\u6808\u548c\u5806\u5185\u5b58\u5206\u914d\uff0c\u4ee5\u53ca Go \u7684\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u80fd\u591f\u4f18\u5316\u6027\u80fd\u3002<\/li>\n<li><strong>defer\u3001panic\u3001recover<\/strong>\uff1a\u638c\u63e1\u5f02\u5e38\u5904\u7406\uff0c\u7406\u89e3 defer \u7528\u4e8e\u8d44\u6e90\u7ba1\u7406\u3001panic \u89e6\u53d1\u9519\u8bef\u3001recover \u6062\u590d\u9519\u8bef\u7684\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<h3>2. <strong>Go \u6807\u51c6\u5e93\u4e0e\u5e38\u7528\u5305<\/strong><\/h3>\n<ul>\n<li><strong>net\/http<\/strong>\uff1aGo \u5185\u7f6e\u7684 HTTP \u5305\u662f\u540e\u7aef\u670d\u52a1\u5f00\u53d1\u7684\u57fa\u7840\uff0c\u5b66\u4e60\u5982\u4f55\u6784\u5efa\u9ad8\u6027\u80fd\u7684 REST API\uff0c\u5982\u4f55\u8fdb\u884c\u4e2d\u95f4\u4ef6\u5f00\u53d1\u548c\u5982\u4f55\u5904\u7406\u8bf7\u6c42\u3001\u54cd\u5e94\u3002<\/li>\n<li><strong>database\/sql<\/strong>\uff1a\u7406\u89e3\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7684\u7ba1\u7406\u3001\u5982\u4f55\u4e0e MySQL\u3001PostgreSQL \u7b49\u5173\u7cfb\u578b\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\uff0c\u5e76\u4f7f\u7528 ORM\uff08\u5982 GORM\uff09\u3002<\/li>\n<li><strong>encoding\/json<\/strong>\uff1a\u5904\u7406 JSON \u683c\u5f0f\u7684\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316\uff0c\u7279\u522b\u662f\u5728\u5fae\u670d\u52a1\u548c API \u7684\u5f00\u53d1\u4e2d\u5341\u5206\u5e38\u89c1\u3002<\/li>\n<li><strong>io \u5305<\/strong>\uff1a\u638c\u63e1\u6d41\u5f0f\u6570\u636e\u5904\u7406\u7684\u6982\u5ff5\uff0c\u5305\u62ec\u6587\u4ef6\u3001\u7f51\u7edc\u6d41\u7b49\u6570\u636e\u7684\u8bfb\u5199\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h3>3. <strong>\u5e76\u53d1\u4e0e\u5206\u5e03\u5f0f\u7cfb\u7edf<\/strong><\/h3>\n<ul>\n<li><strong>Context<\/strong>\uff1aContext \u662f\u5904\u7406\u8d85\u65f6\u3001\u53d6\u6d88\u3001\u8bf7\u6c42\u8303\u56f4\u5185\u6570\u636e\u5171\u4eab\u7684\u6838\u5fc3\u6a21\u5757\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406 HTTP \u8bf7\u6c42\u548c Goroutines \u4e2d\uff0cContext \u662f\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u5de5\u5177\u3002<\/li>\n<li><strong>\u9501\u4e0e\u540c\u6b65<\/strong>\uff1a\u6df1\u5165\u7406\u89e3 Go \u7684\u5e76\u53d1\u63a7\u5236\u5de5\u5177\uff0c\u5982 <code>sync.Mutex<\/code>\u3001<code>sync.RWMutex<\/code>\u3001<code>sync.Once<\/code>\u3001<code>sync.Map<\/code>\uff0c\u5e76\u4e86\u89e3\u5e76\u53d1\u6570\u636e\u7ed3\u6784\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/li>\n<li><strong>Go Channels<\/strong>\uff1a\u4e86\u89e3\u4f7f\u7528 Channel \u8fdb\u884c\u5e76\u53d1\u7f16\u7a0b\u7684\u6a21\u5f0f\uff0c\u5982\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f\u3001\u5de5\u4f5c\u6c60\u6a21\u5f0f\u7b49\uff0c\u638c\u63e1\u5982\u4f55\u901a\u8fc7 Channel \u4f18\u96c5\u5730\u7ba1\u7406\u5e76\u53d1\u6d41\u3002<\/li>\n<\/ul>\n<h3>4. <strong>\u9ad8\u7ea7\u7f51\u7edc\u7f16\u7a0b<\/strong><\/h3>\n<ul>\n<li><strong>GRPC<\/strong>\uff1a\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 Protocol Buffers \u548c GRPC \u6765\u6784\u5efa\u9ad8\u6548\u7684\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff08RPC\uff09\u7cfb\u7edf\uff0c\u7279\u522b\u662f\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u3002<\/li>\n<li><strong>WebSocket \u548c SSE<\/strong>\uff1a\u7406\u89e3\u5982\u4f55\u5904\u7406\u53cc\u5411\u901a\u4fe1\uff0c\u4ee5\u53ca\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u7b49\u5b9e\u65f6\u901a\u8baf\u65b9\u5f0f\u3002<\/li>\n<li><strong>\u53cd\u5411\u4ee3\u7406<\/strong>\uff1a\u5b66\u4e60 Go \u4e2d\u7684\u53cd\u5411\u4ee3\u7406\u6a21\u5f0f\uff0c\u5982\u4f55\u5904\u7406\u8d1f\u8f7d\u5747\u8861\u548c API \u7f51\u5173\u529f\u80fd\u3002<\/li>\n<\/ul>\n<h3>5. <strong>\u6027\u80fd\u8c03\u4f18\u4e0e\u5de5\u5177<\/strong><\/h3>\n<ul>\n<li><strong>pprof \u548c trace<\/strong>\uff1aGo \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6027\u80fd\u8c03\u4f18\u5de5\u5177\uff0c\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 <code>pprof<\/code> \u8fdb\u884c\u6027\u80fd\u5206\u6790\uff0c\u8bca\u65ad CPU \u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u4f18\u5316\u4ee3\u7801\u7684\u6267\u884c\u8def\u5f84\u548c\u5185\u5b58\u5206\u914d\u3002<\/li>\n<li><strong>benchmarking<\/strong>\uff1a\u5b66\u4f1a\u7f16\u5199\u57fa\u51c6\u6d4b\u8bd5\uff0c\u5e76\u4f7f\u7528 <code>testing.B<\/code> \u6765\u8861\u91cf\u4ee3\u7801\u6027\u80fd\uff0c\u8bc6\u522b\u74f6\u9888\u3002<\/li>\n<\/ul>\n<h3>6. <strong>\u5fae\u670d\u52a1\u4e0e\u5206\u5e03\u5f0f\u7cfb\u7edf<\/strong><\/h3>\n<ul>\n<li><strong>\u5fae\u670d\u52a1\u67b6\u6784<\/strong>\uff1a\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 Go \u6765\u8bbe\u8ba1\u548c\u5b9e\u73b0\u5fae\u670d\u52a1\u67b6\u6784\uff0c\u638c\u63e1\u670d\u52a1\u6ce8\u518c\u3001\u53d1\u73b0\u3001\u5065\u5eb7\u68c0\u67e5\u548c\u7194\u65ad\u673a\u5236\u7b49\u3002<\/li>\n<li><strong>\u6d88\u606f\u961f\u5217<\/strong>\uff1a\u7406\u89e3\u5982\u4f55\u4e0e Kafka\u3001RabbitMQ \u7b49\u6d88\u606f\u961f\u5217\u7cfb\u7edf\u96c6\u6210\uff0c\u5904\u7406\u6d88\u606f\u4f20\u9012\u3001\u961f\u5217\u6d88\u8d39\u7b49\u3002<\/li>\n<li><strong>\u5bb9\u5668\u5316\u4e0e\u96c6\u7fa4\u7ba1\u7406<\/strong>\uff1a\u638c\u63e1 Docker \u548c Kubernetes \u7684\u4f7f\u7528\uff0c\u4e86\u89e3\u5982\u4f55\u90e8\u7f72\u548c\u7ba1\u7406\u5206\u5e03\u5f0f Go \u670d\u52a1\uff0c\u5305\u62ec\u8d1f\u8f7d\u5747\u8861\u3001\u670d\u52a1\u7f51\u683c\u7b49\u3002<\/li>\n<\/ul>\n<h3>7. <strong>\u5b89\u5168\u6027<\/strong><\/h3>\n<ul>\n<li><strong>\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743<\/strong>\uff1a\u5b66\u4e60\u5982\u4f55\u96c6\u6210 OAuth 2.0\u3001JWT \u548c OpenID Connect \u7b49\u8ba4\u8bc1\u548c\u6388\u6743\u534f\u8bae\u3002<\/li>\n<li><strong>\u5b89\u5168\u7f16\u7801<\/strong>\uff1a\u7406\u89e3\u5982\u4f55\u907f\u514d\u5e38\u89c1\u7684\u5b89\u5168\u6f0f\u6d1e\uff0c\u4f8b\u5982 SQL \u6ce8\u5165\u3001\u8de8\u7ad9\u811a\u672c\uff08XSS\uff09\u653b\u51fb\u7b49\u3002<\/li>\n<li><strong>\u52a0\u5bc6\u548c\u8bc1\u4e66\u7ba1\u7406<\/strong>\uff1a\u638c\u63e1 <code>crypto<\/code> \u5e93\uff0c\u7406\u89e3\u6570\u636e\u52a0\u5bc6\u3001\u7b7e\u540d\u4ee5\u53ca\u5982\u4f55\u5904\u7406 HTTPS\/TLS \u8fde\u63a5\u3002<\/li>\n<\/ul>\n<h3>8. <strong>\u7cfb\u7edf\u76d1\u63a7\u4e0e\u65e5\u5fd7<\/strong><\/h3>\n<ul>\n<li><strong>\u65e5\u5fd7\u6846\u67b6<\/strong>\uff1a\u4f7f\u7528\u5982 <code>logrus<\/code> \u6216 <code>zap<\/code> \u8fdb\u884c\u9ad8\u6548\u7684\u7ed3\u6784\u5316\u65e5\u5fd7\u8bb0\u5f55\u3002<\/li>\n<li><strong>\u76d1\u63a7\u4e0e\u544a\u8b66<\/strong>\uff1a\u96c6\u6210 Prometheus\u3001Grafana \u7b49\u76d1\u63a7\u7cfb\u7edf\uff0c\u5b9e\u65f6\u8ffd\u8e2a\u7cfb\u7edf\u7684\u8fd0\u884c\u72b6\u6001\uff0c\u8bbe\u7f6e\u5408\u7406\u7684\u76d1\u63a7\u548c\u544a\u8b66\u7b56\u7565\u3002<\/li>\n<\/ul>\n<h3>9. <strong>\u6d4b\u8bd5<\/strong><\/h3>\n<ul>\n<li><strong>\u5355\u5143\u6d4b\u8bd5\u4e0e\u96c6\u6210\u6d4b\u8bd5<\/strong>\uff1a\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 Go \u7684 <code>testing<\/code> \u5305\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u3001\u96c6\u6210\u6d4b\u8bd5\uff0c\u5e76\u4f7f\u7528 <code>gomock<\/code> \u7b49\u5de5\u5177\u8fdb\u884c\u4f9d\u8d56\u6ce8\u5165\u548c mock \u6d4b\u8bd5\u3002<\/li>\n<li><strong>CI\/CD<\/strong>\uff1a\u638c\u63e1\u6301\u7eed\u96c6\u6210\u548c\u6301\u7eed\u4ea4\u4ed8\uff08CI\/CD\uff09\u6d41\u7a0b\uff0c\u96c6\u6210\u5de5\u5177\u5982 Jenkins\u3001GitLab CI \u6216 GitHub Actions\uff0c\u81ea\u52a8\u5316\u6d4b\u8bd5\u4e0e\u53d1\u5e03\u3002<\/li>\n<\/ul>\n<p>\u91cd\u70b9\u5b66\u4e60\u8fd9\u4e9b\u9886\u57df\u7684\u6a21\u5757\uff0c\u80fd\u591f\u8ba9\u4f60\u66f4\u597d\u5730\u638c\u63e1 Go \u7684\u7279\u6027\uff0c\u5e76\u63d0\u5347\u5728\u540e\u7aef\u5f00\u53d1\u4e2d\u7684\u5e94\u7528\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u80dc\u4efb Go \u8bed\u8a00\u7684\u8d44\u6df1\u540e\u7aef\u5f00\u53d1\u5de5\u7a0b\u5e08\u804c\u4f4d\uff0c\u5173\u952e\u7279\u6027\u548c\u6a21\u5757\u7684\u5b66\u4e60\u5e94\u8be5\u805a\u7126\u4e8e\u4ee5\u4e0b\u9886\u57df\uff1a 1. Go \u57fa\u7840\u7279\u6027 G&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6577\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[],"class_list":["post-6577","post","type-post","status-publish","format-standard","hentry","category-123"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6577"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6577\/revisions"}],"predecessor-version":[{"id":6578,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6577\/revisions\/6578"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}