{"id":10006758,"date":"2022-10-28T12:59:24","date_gmt":"2022-10-28T04:59:24","guid":{"rendered":"https:\/\/ityet.com\/?p=10006758"},"modified":"2022-10-28T12:59:24","modified_gmt":"2022-10-28T04:59:24","slug":"go-%e5%a4%a7%e4%bd%ac%e8%89%af%e5%bf%83%e5%8f%91%e7%8e%b0%ef%bc%8c%e6%84%bf%e6%84%8f%e7%bb%99-map-%e5%8a%a0%e6%b8%85%e9%99%a4%e4%ba%86%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006758","title":{"rendered":"Go \u5927\u4f6c\u826f\u5fc3\u53d1\u73b0\uff0c\u613f\u610f\u7ed9 map \u52a0\u6e05\u9664\u4e86\uff1f"},"content":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u714e\u9c7c\u3002<\/p>\n<p>\u4e00\u4e2a\u4e1c\u897f\u6765\u6765\u56de\u56de\u7684\u8ba8\u8bba\uff0c\u5173\u4e86\u53c8\u5f00\uff0c\u5173\u4e86\u540e\u5efa\u65b0\u7684\uff0c\u65b0\u7684\u88ab ban \u4e86\uff0c\u53c8\u53d1\u73b0\u65b0\u7684\u8bba\u636e\uff0c\u518d\u6253\u5f00\u65b0\u7684\u3002\u8fd9\u5728\u804c\u573a\u5de5\u4f5c\u4e2d\u5f88\u5e38\u89c1\uff0c\u5728 Go \u7684\u63d0\u6848\u8ba8\u8bba\u4e2d\uff0c\u4e5f\u51fa\u73b0\u4e86\u3002<\/p>\n<p>\u4eca\u5929\u8981\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u7684\u662f map \u5728 NaN \u4e0a\u7684\u4e00\u4e2a\u5c0f\u4e89\u8bae\u548c\u53ef\u80fd\u5373\u5c06\u51fa\u73b0\u7684 API \u589e\u52a0\u3002<\/p>\n<h2>\u80cc\u666f\u548c\u8003\u9898<\/h2>\n<h3>NaN \u662f\u4ec0\u4e48<\/h3>\n<p>\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\uff0c\u6709\u4e00\u4e2a\u795e\u5947\u7684\u503c,\u53eb\u505a\uff1a<strong>NaN\uff08Not a Number\uff0c\u975e\u6570\uff09\u3002\u5b83\u662f\u6570\u503c\u6570\u636e\u7c7b\u578b\u7684\u4e00\u7c7b\u503c\uff0c\u8868\u793a\u672a\u5b9a\u4e49\u6216\u4e0d\u53ef\u8868\u793a\u7684\u503c<\/strong>\u3002\u5e38\u5728\u6d6e\u70b9\u6570\u8fd0\u7b97\u4e2d\u4f7f\u7528\u3002\u9996\u6b21\u5f15\u5165 NaN \u7684\u662f 1985 \u5e74\u7684 IEEE 754 \u6d6e\u70b9\u6570\u6807\u51c6\u3002<\/p>\n<p>\u5728\u4e0e NaN \u503c\u7684\u5b58\u50a8\u548c\u6bd4\u8f83\u65f6\uff0c\u4f1a\u6709\u95ee\u9898\u3002\u56e0\u4e3a\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u4e3a NaN \u65f6\uff0c\u4e0d\u80fd\u901a\u8fc7\u5224\u65ad x=NaN \u6216 x\u2260NaN \u6765\u8fdb\u884c\u6bd4\u8f83\u3002\u4f46\u56e0\u4e3a NaN \u6c38\u8fdc\u4e0d\u7b49\u4e8e\u5176\u81ea\u8eab\uff0c\u56e0\u6b64\u53ef\u901a\u8fc7\u5224\u65ad x=x \u6216 x\u2260x \u6765\u5224\u65ad x \u662f\u5426\u4e3a NaN \u503c\uff0c\u5c06\u4f1a\u5206\u522b\u8fd4\u56de False \u548c True\u3002<\/p>\n<p>\u5f53 NaN \u4e0e\u53e6\u4e00\u4e2a\u6d6e\u70b9\u6570 x\uff08\u5176\u4e2d x \u53ef\u4e3a\u6b63\u5e38\u503c\u3001\u6b63\u8d1f\u65e0\u7a77\u5927\u6216 NaN\uff09\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u6bd4\u8f83\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u6bd4\u8f83<\/th>\n<th>\u7ed3\u679c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>NaN \u2265 x<\/td>\n<td>False<\/td>\n<\/tr>\n<tr>\n<td>NaN \u2264 x<\/td>\n<td>False<\/td>\n<\/tr>\n<tr>\n<td>NaN &gt; x<\/td>\n<td>False<\/td>\n<\/tr>\n<tr>\n<td>NaN &lt; x<\/td>\n<td>False<\/td>\n<\/tr>\n<tr>\n<td>NaN = x<\/td>\n<td>False<\/td>\n<\/tr>\n<tr>\n<td>NaN \u2260 x<\/td>\n<td>True<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc\u7684\u7406\u89e3\u5bf9\u4e8e\u4e0b\u9762\u7684\u8003\u9898\u5f88\u91cd\u8981\u3002<\/p>\n<h3>Go IEEE-754 \u8003\u9898<\/h3>\n<p>\u5728 Go101 \u4e0a\u770b\u5230\u4e00\u9053\u5173\u4e8e IEEE-754 \u6d6e\u70b9\u6570\u7684\u9898\uff0c\u6211\u8fd9\u91cc\u5f15\u7528\u9898\u76ee\uff0c\u4ee5\u4e0b\u4ee3\u7801\u8f93\u51fa\u4ec0\u4e48\uff1f<\/p>\n<p>\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"go\">package main\n\nimport &quot;math&quot;\n\nfunc main() {\n    a, b, c := 2.0, 1.0, 0.0\n    x, y := a\/c, b\/c     \/\/ infinity\n    n := math.NaN()      \/\/ not a number\n    m := math.Sqrt(-1.0) \/\/ not a number\n    println(x == y, m == n)\n}<\/code><\/pre>\n<ul>\n<li>A\uff1atrue true\u3002<\/li>\n<li>B\uff1atrue false\u3002<\/li>\n<li>C\uff1afalse false\u3002<\/li>\n<li>D\uff1afalse true\u3002<\/li>\n<\/ul>\n<p>\u7b54\u6848\u662f\u5565\uff1f\u662f A \u5417\uff0c\u8fd8\u662f D\uff1f<\/p>\n<p>\u5bf9\u4e0a\u8ff0\u7a0b\u5e8f\u8fdb\u884c\u89e3\u6790\uff0c\u53d8\u91cf x\uff0cy \u662f +Inf \u6b63\u65e0\u7a77\u3002m\uff0cn \u662f NaN \u65e0\u7a77\u503c\u3002<\/p>\n<p>\u6b63\u786e\u7684\u7b54\u6848\u662f\uff1aB\u3002<\/p>\n<p>\u4f60\u7b54\u5bf9\u4e86\u5417\uff1f<\/p>\n<h2>\u63d0\u6848<\/h2>\n<p>\u5728\u5bf9 NaN \u6709\u4e86\u57fa\u672c\u7684\u4e86\u89e3\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u6b63\u5f0f\u8fdb\u5165\u4e3b\u9898\u4e86\u3002\u5728 Go map \u5173\u8054\u63d0\u6848\u4e2d\uff0c\u5e38\u63d0\u5230\u65b0\u589e API\uff0c\u7528\u4e8e\u6ee1\u8db3\u6e05\u7a7a map \u7684\u8bc9\u6c42\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042707452\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4f46\u5728\u591a\u6b21\u8ba8\u8bba\u4e2d\uff0cGo \u5b98\u65b9\u56e2\u961f\u7ed9\u51fa\u7684\u89e3\u51b3\u65b9\u6848\u662f\uff1a<\/p>\n<pre><code class=\"go\">for k := range m {\n    delete(m, k)\n}<\/code><\/pre>\n<p>\u5e76\u5173\u95ed\u4e86\u76f8\u5173\u7684\u63d0\u6848\uff0c\u7ed3\u675f\u4e86\u8fd9\u4e2a\u8bae\u9898\u3002\u7559\u4e0b\u6ee1\u8138 \u201d\u597d\u5427\uff0c\u8fd9\u90fd\u884c\u201c \u7684\u7591\u60d1\u6253\u5de5\u4eba\u7684\u6211\u4eec\uff0c\u8fd9\u662f\u8fd9\u7c7b\u63d0\u6848\u7684\u80cc\u666f\u3002<\/p>\n<p>\u4f46\u8fd9\u5757\u6709\u4e00\u4e2a\u5751\uff0c<strong>\u5728\u5305\u542b\u4efb\u4f55 NaN \u952e\u503c\u65f6\uff0c\u5c06\u65e0\u6cd5\u901a\u8fc7\u5faa\u73af delete \u7684\u65b9\u5f0f\u6e05\u7a7a map<\/strong>\u3002\u4e00\u65e6\u4f60 map \u6709 NaN\uff0c\u4f46\u4f60\u53c8 for+delete\uff0c\u4ee5\u4e3a\u5220\u6389\u4e86\uff0c\u5176\u5b9e\u5e76\u6ca1\u6709\uff0c\u5c31\u4f1a\u4ea7\u751f\u7c7b\u4f3c\u6cc4\u9732\u7684\u6548\u679c\u3002<\/p>\n<p>\u56e0\u6b64 Go \u56e2\u961f\u7684\u7075\u9b42\u4eba\u7269 Russ Cox\uff0c\u91cd\u65b0\u53d1\u8d77\u4e86\u65b0\u63d0\u6848\u300a<a href=\"https:\/\/link.segmentfault.com\/?enc=Jooi48PGSVPuF5TWNnZt7w%3D%3D.nXEpR3mSuqokU1E0K%2FctPxZoJ4a3DdvDbBFMJJVR4%2FZixZxJA71CgS8Xobr92jk2\" rel=\"nofollow\" title=\"proposal: spec: add delete(m\">proposal: spec: add delete(m) to clear map<\/a> to clear map&#8221;)\u300b\u3002\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042707453\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5e0c\u671b\u501f\u6b64\u6765\u89e3\u51b3 map \u5728 NaN \u7684\u95ee\u9898\uff0c\u5e76\u540c\u65f6\u5b8c\u6210\u4e00\u76f4\u4ee5\u6765\u8ba8\u8bba\u7684 map \u6e05\u7a7a\/\u91cd\u7f6e\/\u6e05\u7406\u7b49\u793e\u533a\u8bc9\u6c42\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u65b0\u589e\uff0c\u5982\u4e0b\u7279\u6027\uff1a<\/p>\n<pre><code class=\"go\">delete(m)<\/code><\/pre>\n<p>\u652f\u6301\u6e05\u9664 map \u7684\u529f\u80fd\uff08\u5373\u4f7f\u5305\u542b NaN\uff09\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u9488\u5bf9\u8fd9\u4e2a\u63d0\u6848\u4e5f\u6709\u51e0\u79cd\u58f0\u97f3\uff0c\u5206\u522b\u662f\u4e07\u6076\u7684\u547d\u540d\uff0c\u5bf9\u53eb delete\uff0c\u8fd8\u662f\u53eb clear \u90fd\u6709\u7740\u4e0d\u540c\u7684\u89c1\u89e3\u3002<\/p>\n<pre><code class=\"go\">\/\/ Clear removes all entries from m, leaving it empty.\nfunc Clear[M ~map[K]V, K comparable, V any](m M &quot;M ~map[K]V, K comparable, V any&quot;)\n\ndelete(m)<\/code><\/pre>\n<p>\u4e5f\u6709\u58f0\u97f3\u63d0\u5230\u4e0d\u5141\u8bb8\u5f15\u5165 NaN \u503c\uff0c\u4f46\u663e\u7136\u3002\u5728 Go1 \u5df2\u7ecf\u5f88\u96be\u4e86\uff0c\u56e0\u4e3a NaN \u5df2\u7ecf\u88ab\u5141\u8bb8\u5f15\u5165\uff0c\u7403\u5df2\u7ecf\u5728\u9505\u91cc\u4e86\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042707454\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5bf9\u4e8e map \u65b0\u589e API \u7528\u4e8e\u6e05\u7a7a\/\u91cd\u7f6e\/\u6e05\u9664\u7684\u4f5c\u7528\uff0c\u4f60\u600e\u4e48\u770b\u5462\uff1f\u8fd8\u662f\u8bf4\u4f60\u4e5f\u66f4\u559c\u6b22\u5bf9 NaN \u5355\u72ec\u7684\u5904\u7406\uff1f\u4f8b\u5982 panic\uff1f<\/p>\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 href=\"https:\/\/link.segmentfault.com\/?enc=k94%2BAXqlqfOag4OIbU0Kig%3D%3D.XFEASOyOPVwY0jNNtolRrRqL8ISXCiIS%2BnHHxT%2FGQt4%3D\" rel=\"nofollow\">github.com\/eddycjy\/blog<\/a> \u5df2\u6536\u5f55\uff0c\u5b66\u4e60 Go \u8bed\u8a00\u53ef\u4ee5\u770b <a href=\"https:\/\/link.segmentfault.com\/?enc=ZJ4mddYV9lZUtEokEz0Mcg%3D%3D.SuxvLxDk8f3dADO5NKdoqZhLpsqVsLqs5lrIR%2FT1iPxR8S0D0EzISQ7PDLNYCnOG\" rel=\"nofollow\">Go \u5b66\u4e60\u5730\u56fe\u548c\u8def\u7ebf<\/a>\uff0c\u6b22\u8fce Star \u50ac\u66f4\u3002<\/p><\/blockquote>\n<h2>\u63a8\u8350\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=MhgWSlPC45JCskiWHsssjQ%3D%3D.w%2FT9wJjrR30a63TTbxlKviPEHuuyHNlcbThWvb4TANkQCKp9PiE8u91tI8Z%2FeMeAyWaAf%2FQkGGYqH%2BtZzTpBmw%3D%3D\" rel=\"nofollow\">Go \u53ea\u4f1a if err != nil\uff1f\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u5206\u4eab\u8fd9\u4e9b\u4f18\u96c5\u7684\u5904\u7406\u59ff\u52bf\u7ed9\u4f60\uff01<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=7ujzE%2Btsxdo8%2B6kbXCmoMg%3D%3D.XMPcSXInYVUX1NKckbhpY05%2Fo5AlUVex7JeqS80V2X1AvSFELREvB1sO6%2BPaZUequeYvqtznPu1lskini10Yyg%3D%3D\" rel=\"nofollow\">Go \u9519\u8bef\u5904\u7406\u65b0\u601d\u8def\uff1f\u7528\u5de6\u4fa7\u51fd\u6570\u548c\u8868\u8fbe\u5f0f<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=7gKCeGvFe0v0xJeTwg5rpQ%3D%3D.s2RPBAV0SUzNr1hi8AaUsM6gQcRzLBPN2XqXm4mNS2qgAqo1GUViTDayzqlOdXCUPn17XocwhFtnKuGjDrEIOg%3D%3D\" rel=\"nofollow\">\u5148\u7779\u4e3a\u5feb\uff0cGo2 Error \u7684\u6323\u624e\u4e4b\u8def<\/a><\/li>\n<\/ul>\n<h3>Go \u56fe\u4e66\u7cfb\u5217<\/h3>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=IC1N4vfaklWStpkVMN9krw%3D%3D.JRcDvBHbsuxPkPA9j42BI8I%2FDTRmjXTAGy8nfpBPnED0SFZYb2NFGeDAL9f0tNZZ\" rel=\"nofollow\">Go \u8bed\u8a00\u5165\u95e8\u7cfb\u5217\uff1a\u521d\u63a2 Go \u9879\u76ee\u5b9e\u6218<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=IhFRujLps6uJiWuboY%2BRCw%3D%3D.VqnjOy1Y7M3kiUxrIVgr4NGe4ZFd2Yxx8VSkeNywBZg%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u7f16\u7a0b\u4e4b\u65c5\uff1a\u6df1\u5165\u7528 Go \u505a\u9879\u76ee<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=tU8Y9lepYhkLYe4F74CE2A%3D%3D.8WidgAatbfINt%2F4zf02mb%2FzkjLRHIBFhIzDhM%2FeW%2BPs%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u8bbe\u8ba1\u54f2\u5b66\uff1a\u4e86\u89e3 Go \u7684\u4e3a\u4ec0\u4e48\u548c\u8bbe\u8ba1\u601d\u8003<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=VXIjJ3AJ%2B8kFT5Md0SS%2BtQ%3D%3D.q%2BTnczm9TTPmX43s00s7hFVz%2F6IUfndHBjvD%2FiC27To%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u8fdb\u9636\u4e4b\u65c5\uff1a\u8fdb\u4e00\u6b65\u6df1\u5165 Go \u6e90\u7801<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042707450\">https:\/\/segmentfault.com\/a\/1190000042707450<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u714e\u9c7c\u3002 \u4e00\u4e2a\u4e1c\u897f\u6765\u6765\u56de\u56de\u7684\u8ba8\u8bba\uff0c\u5173\u4e86\u53c8\u5f00\uff0c\u5173\u4e86\u540e\u5efa\u65b0\u7684\uff0c\u65b0\u7684\u88ab ban \u4e86\uff0c\u53c8\u53d1\u73b0\u65b0\u7684\u8bba\u636e\uff0c\u518d\u6253\u5f00 [&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-10006758","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\/10006758","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=10006758"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006758\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}