{"id":207926,"date":"2025-07-08T08:33:22","date_gmt":"2025-07-08T00:33:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207926\/"},"modified":"2025-07-08T08:33:22","modified_gmt":"2025-07-08T00:33:22","slug":"%e5%a6%82%e4%bd%95%e6%a3%80%e6%b5%8b%e5%b8%a6%e6%9c%89errors-is%e7%9a%84json-syntaxerror","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207926\/","title":{"rendered":"\u5982\u4f55\u68c0\u6d4b\u5e26\u6709errors.Is\u7684json.SyntaxError"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u68c0\u6d4b\u5e26\u6709errors.Is\u7684json.SyntaxError<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 21:06:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u68c0\u6d4b\u5e26\u6709errors.Is\u7684json.SyntaxError\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728\u68c0\u67e5\u89e3\u7801\u5668\u5931\u8d25\u8fd4\u56de\u7684\u9519\u8bef\u65f6\uff0c\u6211\u65e0\u6cd5\u68c0\u6d4b\u5230 json.syntaxerror\u3002\u89e3\u7801\u64cd\u4f5c\uff01<\/p>\n<p>\u5728\u8fd9\u91cc\u60a8\u53ef\u4ee5\u770b\u5230\u6f14\u793a\u4e2d\u7684\u4e00\u4e2a\u5de5\u4f5c\u793a\u4f8b\u3002<\/p>\n<p>\u6b63\u5982\u4f60\u6240\u770b\u5230\u7684\uff0c\u8c03\u8bd5\u5668\u5411\u6211\u786e\u8ba4\u5b83\u662f\u4e00\u4e2a\u6307\u5411 json.syntaxerror \u7684\u6307\u9488\uff0c\u4f46errors.is \u65e0\u6cd5\u68c0\u6d4b\u5230\u5b83\u3002<\/p>\n<p>\u6211\u68c0\u67e5\u4e86\u9519\u8bef\u3002\u5b9e\u65bd\u60c5\u51b5\uff1a<\/p>\n<pre>func Is(err, target error) bool {\n    if target == nil {\n        return err == target\n    }\n\n    isComparable := reflectlite.TypeOf(target).Comparable()\n    for {\n        if isComparable &amp;&amp; err == target {\n            return true\n        }\n        if x, ok := err.(interface{ Is(error) bool }); ok &amp;&amp; x.Is(target) {\n            return true\n        }\n        \/\/ TODO: consider supporting target.Is(err). This would allow\n        \/\/ user-definable predicates, but also may allow for coping with sloppy\n        \/\/ APIs, thereby making it easier to get away with them.\n        if err = Unwrap(err); err == nil {\n            return false\n        }\n    }\n}<\/pre>\n<p>\u5b83\u4eec\u662f\u53ef\u6bd4\u8f83\u7684\uff08iscomparable \u53d8\u91cf\u4e3a true\uff09\uff0c\u4f46\u662f\uff0c\u5f53\u6211\u671f\u671b\u5b83\u5728\u6267\u884c <code>if iscomparable &amp;&amp; err == target {<\/code> \u65f6\u8fd4\u56de true \u65f6\uff0c\u5b83\u200b\u200b\u4f1a\u7ee7\u7eed&#8230;<\/p>\n<p>\u6211\u505a\u9519\u4e86\u4ec0\u4e48\uff1f \u63d0\u524d\u81f4\u8c22\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5f53\u524d\u53d1\u751f\u7684\u60c5\u51b5\u662f\uff0c\u60a8\u5c06\u65b0 <code>json.syntaxerror<\/code> \u7684\u5185\u5b58\u5730\u5740\u4e0e\u4ece <code>decode<\/code> \u8fd4\u56de\u7684\u9519\u8bef\u8fdb\u884c\u6bd4\u8f83\u3002\u6b63\u5982\u60a8\u6240\u6ce8\u610f\u5230\u7684\uff0c\u8fd9\u6c38\u8fdc\u4e0d\u4f1a\u662f\u771f\u7684\u3002<\/p>\n<p>\u60a8\u60f3\u8981\u505a\u7684\u6709\u70b9\u4e0d\u540c\uff1a\u68c0\u67e5 <code>err<\/code> \u662f\u5426\u5c5e\u4e8e <code>syntaxerror<\/code> \u7c7b\u578b\uff0c\u7136\u540e\u76f4\u63a5\u4f7f\u7528\u8be5\u5bf9\u8c61\u3002\u8fd9\u53ef\u4ee5\u4f7f\u7528 \u6765\u5b9e\u73b0\uff0c\u5b83\u57fa\u672c\u4e0a\u68c0\u67e5\u63a5\u53e3\u7684\u57fa\u7840\u7c7b\u578b\uff08\u5728\u672c\u4f8b\u4e2d\u4e3a <code>error<\/code>\uff09\u662f\u5426\u662f\u66f4\u5177\u4f53\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u8fd9\u5c31\u662f \u7684\u4f5c\u7528\u3002\u5b83\u586b\u5145\u60a8\u6307\u5b9a\u7684\u7279\u5b9a\u9519\u8bef\u7c7b\u578b\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>if err != nil {\n    var serr *json.SyntaxError\n    \n    if errors.As(err, &amp;serr) {\n        fmt.Println(\"Syntax error:\", serr)\n        fmt.Println(\"Offset:\", serr.Offset)\n    } else {\n        fmt.Println(\"Other error:\", err)\n    }\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u68c0\u6d4b\u5e26\u6709errors.Is\u7684json.SyntaxError\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207926","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207926"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207926\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}