{"id":10008352,"date":"2024-02-11T19:56:06","date_gmt":"2024-02-11T11:56:06","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008352"},"modified":"2024-02-11T19:56:06","modified_gmt":"2024-02-11T11:56:06","slug":"go1-22-%e6%ad%a3%e5%bc%8f%e5%8f%91%e5%b8%83%ef%bc%81%e5%8c%85%e5%90%ab%e8%af%ad%e8%a8%80%e5%8f%98%e5%8c%96%e3%80%81%e6%80%a7%e8%83%bd%e6%8f%90%e9%ab%98%e3%80%81%e6%a0%87%e5%87%86%e5%ba%93%e5%8f%98","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008352","title":{"rendered":"Go1.22 \u6b63\u5f0f\u53d1\u5e03\uff01\u5305\u542b\u8bed\u8a00\u53d8\u5316\u3001\u6027\u80fd\u63d0\u9ad8\u3001\u6807\u51c6\u5e93\u53d8\u52a8\u7b49\u91cd\u8981\u7279\u6027"},"content":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u714e\u9c7c\u3002<\/p>\n<p><strong>\u4eca\u5929 Go1.22 \u6b63\u5f0f\u53d1\u5e03\u4e86\uff01<\/strong><\/p>\n<p>\u4e00\u65e9\u8d77\u6765\uff0c\u8fd8\u6ca1\u51fa\u95e8\u5c31\u8d76\u7d27\u6574\u7406\u4e86\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u3002\u4e00\u8d77\u5b66\u4e60\u65b0\u7248\u672c\uff01<\/p>\n<p>\u4e4b\u524d\u6211\u8fd8\u770b\u8fdb\u5ea6\u9884\u6d4b 2 \u6708\u4e2d\u53d1\u5e03\u3002\u6ca1\u60f3\u5230\u76f4\u63a5\u6253\u8138\u3002Go \u6838\u5fc3\u56e2\u961f\u6210\u529f\u8d76\u5728\u6625\u8282\u524d\u53d1\u5e03\u4e86\uff01\u771f\u662f\u6709\u70b9\u731b\u3002<\/p>\n<p>Go 1.22 \u65b0\u7248\u672c\u5b89\u88c5\u65b9\u5f0f\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/files.mdnice.com\/user\/3610\/d9621757-3df9-4d1a-9930-55b9057a824a.png\" alt=\"\" title=\"\"><\/p>\n<p>Go1.22 \u5305\u542b\u591a\u9879\u91cd\u8981\u7684\u65b0\u529f\u80fd\u548c\u6539\u8fdb\u3002\u4ee5\u4e0b\u662f\u90e8\u5206\u663e\u8457\u53d8\u5316\uff1b\u5b8c\u6574\u5217\u8868\u8bf7\u67e5\u770b\u300aGo 1.22 Release Notes\u300b\u3002<\/p>\n<p>\u6b22\u8fce\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u65b0\u7248\u672c\u7279\u6027\u548c\u5185\u5bb9\uff01<\/p>\n<h2>\u5b98\u65b9\u6982\u8ff0<\/h2>\n<h3>\u8bed\u8a00\u53d8\u5316<\/h3>\n<p>\u957f\u671f\u5b58\u5728\u7684 &#8220;for &#8220;\u5faa\u73af\u5728\u8fed\u4ee3\u4e4b\u95f4\u610f\u5916\u5171\u4eab\u5faa\u73af\u53d8\u91cf\u7684\u95ee\u9898\u73b0\u5df2\u5f97\u5230\u89e3\u51b3\u3002<\/p>\n<p>\u4ece Go 1.22 \u5f00\u59cb\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6309\u4e00\u5b9a\u987a\u5e8f\u6253\u5370 &#8220;a&#8221;\u3001&#8221;b &#8220;\u548c &#8220;c&#8221;\uff1a<\/p>\n<pre><code class=\"go\">func main() {\n    done := make(chan bool)\n\n    values := []string{&quot;a&quot;, &quot;b&quot;, &quot;c&quot;}\n    for _, v := range values {\n        go func() {\n            fmt.Println(v)\n            done &lt;- true\n        }()\n    }\n\n    \/\/ wait for all goroutines to complete before exiting\n    for _ = range values {\n        &lt;-done\n    }\n}<\/code><\/pre>\n<p>\u6709\u5173\u8fd9\u4e00\u53d8\u66f4\u4ee5\u53ca\u6709\u52a9\u4e8e\u9632\u6b62\u4ee3\u7801\u610f\u5916\u4e2d\u65ad\u7684\u5de5\u5177\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u4e4b\u524d\u7684\u300a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=%2BaI5UCJ%2B5ZHwin59CvIGfA%3D%3D.I81lNopSRsMlAQH%2BNAbFdygVXInmrH8ajOlwdwCbjCvDRHDZlAdXR2zQpZEQ6PB8\" rel=\"noopener noreferrer\">Fixing For Loops in Go 1.22<\/a>\u300b\u3002<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u8bed\u8a00\u53d8\u5316\u662f\u652f\u6301\u6574\u6570\u8303\u56f4\uff1a<\/p>\n<pre><code class=\"go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n    for i := range 10 {\n        fmt.Println(10 - i)\n    }\n    fmt.Println(&quot;go1.22 has lift-off!&quot;)\n}<\/code><\/pre>\n<p>\u7b2c\u4e8c\u4e2a\u8bed\u8a00\u53d8\u5316\u662f\u652f\u6301\u6574\u6570\u8303\u56f4\uff1aa \u5728\u8fd9\u4e2a\u5012\u8ba1\u65f6\u7a0b\u5e8f\u4e2d\uff0ci \u7684\u53d6\u503c\u8303\u56f4\u4e3a 0 \u81f3 9\uff08\u542b 9\uff09\u3002<\/p>\n<h3>\u6027\u80fd\u63d0\u9ad8<\/h3>\n<p>Go \u8fd0\u884c\u65f6\u4e2d\u7684\u5185\u5b58\u4f18\u5316\u53ef\u5c06 CPU \u6027\u80fd\u63d0\u9ad8 1-3%\uff0c\u540c\u65f6\u8fd8\u53ef\u5c06\u5927\u591a\u6570 Go \u7a0b\u5e8f\u7684\u5185\u5b58\u5f00\u9500\u51cf\u5c11\u7ea6 1%\u3002<\/p>\n<p>\u5728 Go 1.21 \u4e2d\uff0c\u6211\u4eec\u4e3a Go \u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u914d\u7f6e\u6587\u4ef6\u5f15\u5bfc\u4f18\u5316 (PGO)\uff0c\u800c\u4e14\u8fd9\u4e00\u529f\u80fd\u8fd8\u5728\u4e0d\u65ad\u6539\u8fdb\u3002<\/p>\n<p>Go 1.22 \u4e2d\u65b0\u589e\u7684\u4f18\u5316\u4e4b\u4e00\u662f\u6539\u8fdb\u4e86\u865a\u62df\u5316\uff0c\u5141\u8bb8\u9759\u6001\u8c03\u5ea6\u66f4\u591a\u7684\u63a5\u53e3\u65b9\u6cd5\u8c03\u7528\u3002\u542f\u7528 PGO \u540e\uff0c\u5927\u591a\u6570\u7a0b\u5e8f\u7684\u6027\u80fd\u5c06\u63d0\u9ad8 2% \u81f3 14%\u3002<\/p>\n<h3>\u6807\u51c6\u5e93\u53d8\u52a8<\/h3>\n<ul>\n<li>\u65b0\u7684 math\/rand\/v2 \u8f6f\u4ef6\u5305\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u3001\u66f4\u4e00\u81f4\u7684\u5e94\u7528\u7a0b\u5e8f\u63a5\u53e3\uff0c\u5e76\u4f7f\u7528\u4e86\u8d28\u91cf\u66f4\u9ad8\u3001\u901f\u5ea6\u66f4\u5feb\u7684\u4f2a\u968f\u673a\u751f\u6210\u7b97\u6cd5\u3002<\/li>\n<li>\n<p><code>net\/http.ServeMux<\/code> \u4f7f\u7528\u7684\u6a21\u5f0f\u73b0\u5728\u53ef\u63a5\u53d7\u65b9\u6cd5\u548c\u901a\u914d\u7b26\u3002<\/p>\n<ul>\n<li>\u4f8b\u5982\uff1a\u8def\u7531\u5668\u63a5\u53d7 <code>GET \/task\/{id}\/<\/code> \u8fd9\u6837\u7684\u6a21\u5f0f\uff0c\u8be5\u6a21\u5f0f\u53ea\u5339\u914d GET \u8bf7\u6c42\uff0c\u5e76\u5728\u4e00\u4e2a\u53ef\u901a\u8fc7 Request \u503c\u8bbf\u95ee\u7684\u6620\u5c04\u4e2d\u6355\u83b7 {id} \u6bb5\u7684\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n<li>database\/sql \u5305\u4e2d\u65b0\u589e\u4e86 <code>Null[T]<\/code> \u7c7b\u578b\uff0c\u4e3a\u626b\u63cf\u53ef\u4e3a\u7a7a\u7684\u5217\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6cd5\u3002<\/li>\n<li>\u5728 slices \u5305\u4e2d\u6dfb\u52a0\u4e86 Concat \u51fd\u6570\uff0c\u7528\u4e8e\u8fde\u63a5\u4efb\u610f\u7c7b\u578b\u7684\u591a\u4e2a\u7247\u6bb5\u3002<\/li>\n<\/ul>\n<h2>\u8be6\u7ec6\u89e3\u8bfb Go1.22 \u7279\u6027<\/h2>\n<ul>\n<li><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=jvLKoHz75xN5uzDB6YTcZA%3D%3D.%2BNXWqzgrcqNErTc4QaBeWuhxO2X3AZ19P%2BWavp%2BK5G7MvA0myF8H3xyOr8iLqxUYJwS4rxOeWg8rmUH8yJOJnw%3D%3D\" rel=\"noopener noreferrer\">Go1.22 \u65b0\u7279\u6027\uff1a\u589e\u5f3a http.ServerMux \u8def\u7531\u80fd\u529b\uff0c\u5c06\u6709\u66f4\u5f3a\u7684\u8868\u73b0\u529b\uff01<\/a><\/li>\n<li><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=t9EpRp%2FFg5CLO3f74nCzkw%3D%3D.E6fKB9D1i6buNKDkwY82IiAvjgjFashEX%2FrXzWPQ99gLEudwkiy1cBwsr70z1dLN9uE9Aq60rj2z9fQ%2F%2Fz9szg%3D%3D\" rel=\"noopener noreferrer\">Go1.22 \u65b0\u7279\u6027\uff1afor \u5faa\u73af\u4e0d\u518d\u5171\u4eab\u5faa\u73af\u53d8\u91cf\uff0c\u4e14\u652f\u6301\u6574\u6570\u8303\u56f4<\/a><\/li>\n<li><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=TH0ILruXA7SSrKo9JF7j2g%3D%3D.jOvEuypr8pfk1RpIwHp%2Bj2yQ4wGBrT0X1M0pnt%2BgoZ8MKfgH1RZy3%2FihLh2t7xMQ9EX0Lw%2BXGanElEzB0jlv%2BA%3D%3D\" rel=\"noopener noreferrer\">\u4e3a\u4ec0\u4e48 Go1.22 for \u5faa\u73af\u8981\u652f\u6301\u6574\u6570\u8303\u56f4\uff1f<\/a><\/li>\n<li><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=f0MtZxUvrgTTL46Yg%2FPdgA%3D%3D.Y%2BSMB6%2BF5fFvNHccoHNsXH4JP3rjHls%2F6mMQvLYJ5c4mCkESaWW0mZrGmkviuFlwrGdqyxG4GgPhtOHWWs5zKQ%3D%3D\" rel=\"noopener noreferrer\">Go1.22 \u65b0\u7279\u6027\uff1aSlices \u53d8\u66f4 Concat\u3001Delete\u3001Insert \u7b49\u51fd\u6570\uff0c\u5bf9\u5f00\u53d1\u633a\u6709\u5e2e\u52a9\uff01<\/a><\/li>\n<li><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=jfe3716G0Ak%2B%2BVRgbp3oBQ%3D%3D.mb3Kt3arIRsyIwpwpBiyQYsouTIgin9959CLeI8arKlDf01cQe8%2FHoTyfgyrK4LxeCx3AzR6xkdA4GVx7wkE4Q%3D%3D\" rel=\"noopener noreferrer\">Go1.22 \u65b0\u7279\u6027\uff1a\u65b0\u7684 math\/rand\/v2 \u5e93\uff0c\u66f4\u5feb\u66f4\u6807\u51c6\uff01<\/a><\/li>\n<li><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=YQ2bhDq6UX5K%2ByBSX%2F2k3w%3D%3D.FBfJQ9S%2FFskqz0kR4OjUzOUoxKzq7Jl4Ki7e3Wha1kJAwRurEr9ltK6BOwdhbuoA9A5u%2BMP%2FxxstcpF1UP1ZHQ%3D%3D\" rel=\"noopener noreferrer\">Go1.22 \u65b0\u7279\u6027\uff1a\u6027\u80fd\u63d0\u9ad8\u3001Trace \u5927\u4fee\u3001\u5de5\u4f5c\u533a\u652f\u6301 vendor \u7b49<\/a><\/li>\n<\/ul>\n<blockquote><p>\u6587\u7ae0\u6301\u7eed\u66f4\u65b0\uff0c\u53ef\u4ee5\u5fae\u4fe1\u641c\u3010\u8111\u5b50\u8fdb\u714e\u9c7c\u4e86\u3011\u9605\u8bfb\uff0c\u672c\u6587 <strong>GitHub<\/strong> <a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=TJoENsyII4S6oU4ybpLgfA%3D%3D.INmOlrJGU84Wqos9U90LLgDPFXLSsuqwP%2BiiWcNKkV0%3D\" rel=\"noopener noreferrer\">github.com\/eddycjy\/blog<\/a> \u5df2\u6536\u5f55\uff0c\u5b66\u4e60 Go \u8bed\u8a00\u53ef\u4ee5\u770b <a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=GtwNco5ciSr7wKP60b6lBw%3D%3D.XCgPp7fiERwjSxRmfk7RMVgAkU%2FPwj33qVUxOff8Bla9GBkzyfxvTfAf3pxIpBFB\" rel=\"noopener noreferrer\">Go \u5b66\u4e60\u5730\u56fe\u548c\u8def\u7ebf<\/a>\uff0c\u6b22\u8fce Star \u50ac\u66f4\u3002<\/p><\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044628192\">https:\/\/segmentfault.com\/a\/1190000044628192<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u714e\u9c7c\u3002 \u4eca\u5929 Go1.22 \u6b63\u5f0f\u53d1\u5e03\u4e86\uff01 \u4e00\u65e9\u8d77\u6765\uff0c\u8fd8\u6ca1\u51fa\u95e8\u5c31\u8d76\u7d27\u6574\u7406\u4e86\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u3002\u4e00\u8d77\u5b66\u4e60\u65b0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008352","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008352"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008352\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}