{"id":208085,"date":"2025-07-08T12:03:36","date_gmt":"2025-07-08T04:03:36","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208085\/"},"modified":"2025-07-08T12:03:36","modified_gmt":"2025-07-08T04:03:36","slug":"%e6%97%a0%e6%b3%95%e6%a8%a1%e4%bb%bfgolang%e7%9a%84%e9%94%99%e8%af%af%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208085\/","title":{"rendered":"\u65e0\u6cd5\u6a21\u4effGolang\u7684\u9519\u8bef\u63a5\u53e3"},"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>\u65e0\u6cd5\u6a21\u4effGolang\u7684\u9519\u8bef\u63a5\u53e3<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-28 20:51:30<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u65e0\u6cd5\u6a21\u4effGolang\u7684\u9519\u8bef\u63a5\u53e3\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u8bd5\u56fe\u6a21\u4eff\u4e00\u6bb5\u7c7b\u4f3c\u7684\u4ee3\u7801\u4e2d\u53d1\u751f\u7684\u9519\u8bef\uff0c\u7b2c\u4e00\u6bb5\u4ee3\u7801\u6253\u5370 &#8212; log \u7b2c\u4e8c\u4e2a\u4e0d\u6253\u5370\uff0c\u4e3a\u4ec0\u4e48\uff1f\u6216\u8005\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u8bd5\u56fe\u4e86\u89e3\u5982\u4f55\u5728\u6ca1\u6709\u663e\u5f0f\u8c03\u7528\u7684\u60c5\u51b5\u4e0b\u8c03\u7528 error() \u65b9\u6cd5\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n)\n\ntype argError struct {\n    arg  int\n    prob string\n}\n\nfunc (e *argError) Error() string {\n    return fmt.Sprintf(\"%d ---- %s\", e.arg, e.prob)\n}\n\nfunc f2(arg int) (int, error) {\n        return -1, &amp;argError{arg, \"can't work with it\"}\n}\n\nfunc main() {\n    a,b := f2(42)\n    fmt.Println(a)\n    fmt.Println(b)\n}\n\n\n\npackage main\n\nimport (\n    \"fmt\"\n)\n\ntype myerr interface {\n    Apple() string\n}\n\ntype dummy struct {\n    age  int\n    name string\n}\n\nfunc (d *dummy) Apple() string {\n    return fmt.Sprintf(\"%d --- %s\", d.age, d.name)\n}\n\nfunc f1(arg int) (string, myerr) {\n    return \"f1\", &amp;dummy{arg, \"ret\"}\n}\n\nfunc main() {\n    i, j := f1(42)\n    fmt.Println(i)\n    fmt.Println(j)\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u4e3a\u4e86\u4f7f\u5176\u5de5\u4f5c\uff0c\u60a8\u7684\u7c7b\u578b\u5fc5\u987b\u901a\u8fc7 <code>error<\/code> \u65b9\u6cd5\u6765\u5b9e\u73b0 <code>error<\/code> \u63a5\u53e3\u3002\u6211\u5efa\u8bae\u8c03\u6574\u60a8\u7684 <code>myerr<\/code> \u63a5\u53e3\u4ee5\u5d4c\u5165 <code>error<\/code>\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n)\n\ntype myerr interface {\n    error                    \/\/ &lt;--- note: embedding 'error' here\n    Apple() string\n}\n\ntype dummy struct {\n    age  int\n    name string\n}\n\nfunc (d *dummy) Apple() string {\n    return fmt.Sprintf(\"%d --- %s\", d.age, d.name)\n}\n\n\/\/ &lt;--- note: implement the 'error' interface for the 'dummy' type\nfunc (d *dummy) Error() string {\n    return fmt.Sprintf(\"%d ---- %s\", d.age, d.name)\n}\n\nfunc f1(arg int) (string, myerr) {\n    return \"f1\", &amp;dummy{arg, \"ret\"}\n}\n\nfunc main() {\n    i, j := f1(42)\n    fmt.Println(i)\n    fmt.Println(j)\n}<\/pre>\n<p>\u8fd9\u5e94\u8be5\u6309\u7167\u60a8\u7684\u9884\u671f\u8c03\u7528 <code>j<\/code> \u7684 <code>error<\/code> \u65b9\u6cd5\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u8bd5\u8bd5\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u65e0\u6cd5\u6a21\u4effGolang\u7684\u9519\u8bef\u63a5\u53e3\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\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-208085","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208085","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=208085"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208085\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}