{"id":205909,"date":"2025-06-12T11:38:27","date_gmt":"2025-06-12T03:38:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205909\/"},"modified":"2025-06-12T11:38:27","modified_gmt":"2025-06-12T03:38:27","slug":"%e5%9c%a8-golang-%e5%87%bd%e6%95%b0%e4%b8%ad%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e9%94%99%e8%af%af%e7%9a%84%e5%8f%82%e6%95%b0%e8%be%93%e5%85%a5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205909\/","title":{"rendered":"\u5728 Golang \u51fd\u6570\u4e2d\u5982\u4f55\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1f"},"content":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5728 GOLANG \u51fd\u6570\u4e2d\u5982\u4f55\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1f\u300b\uff0c\u8986\u76d6\u4e86GOLANG\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002,Go \u51fd\u6570\u4e2d\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1a\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\uff1a\u521b\u5efa\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\uff0c\u8868\u793a\u65e0\u6548\u7684\u53c2\u6570\u3002\u53c2\u6570\u9a8c\u8bc1\uff1a\u5728\u51fd\u6570\u4f53\u4e2d\u68c0\u67e5\u53c2\u6570\u662f\u5426\u6709\u6548\u3002\u8fd4\u56de\u9519\u8bef\uff1a\u5982\u679c\u53c2\u6570\u65e0\u6548\uff0c\u8fd4\u56de\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730039074671e4d22af292.jpg\" class=\"aligncenter\">,\u5728 Go \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1a,\u4ee5\u4e0b\u662f\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\uff1a,\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u5e94\u7528\u7a0b\u5e8f\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\u6765\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1a,\u8fd0\u884c\u6b64\u5e94\u7528\u7a0b\u5e8f\u5c06\u5bfc\u81f4\u4ee5\u4e0b\u8f93\u51fa\uff1a,\u5728\u8be5\u6848\u4f8b\u4e2d\uff0c\u51fd\u6570 MyFunction \u4f7f\u7528<br \/>\n<code>errors.New<\/code> \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u9519\u8bef\u5bf9\u8c61\uff0c\u5b83\u5305\u542b\u4e00\u4e2a\u8bf4\u660e\u65e0\u6548\u53c2\u6570\u95ee\u9898\u7684\u9519\u8bef\u6d88\u606f\u3002,\u4eca\u5929\u5173\u4e8e\u300a\u5728 GOLANG \u51fd\u6570\u4e2d\u5982\u4f55\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1f\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\u6ce8GOLANG\u516c\u4f17\u53f7\uff01,\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5728 GOLANG \u51fd\u6570\u4e2d\u5982\u4f55\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1f\u300b\uff0c\u8986\u76d6\u4e86GOLANG\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5728 GOLANG \u51fd\u6570\u4e2d\u5982\u4f55\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1f<\/span><\/p>\n<h1>\u5728 GOLANG \u51fd\u6570\u4e2d\u5982\u4f55\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1f<\/h1>\n<p><span>2024-10-27 22:24:49<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5728 GOLANG \u51fd\u6570\u4e2d\u5982\u4f55\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1f\u300b\uff0c\u8986\u76d6\u4e86GOLANG\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p>Go \u51fd\u6570\u4e2d\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1a\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\uff1a\u521b\u5efa\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\uff0c\u8868\u793a\u65e0\u6548\u7684\u53c2\u6570\u3002\u53c2\u6570\u9a8c\u8bc1\uff1a\u5728\u51fd\u6570\u4f53\u4e2d\u68c0\u67e5\u53c2\u6570\u662f\u5426\u6709\u6548\u3002\u8fd4\u56de\u9519\u8bef\uff1a\u5982\u679c\u53c2\u6570\u65e0\u6548\uff0c\u8fd4\u56de\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730039074671e4d22af292.jpg\" class=\"aligncenter\"><\/p>\n<h2>\u5728 Go \u51fd\u6570\u4e2d\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165<\/h2>\n<p>\u5728 Go \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1a<\/p>\n<ol>\n<li><strong>\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\uff1a<\/strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\u6765\u8868\u793a\u65e0\u6548\u7684\u53c2\u6570\u3002<\/li>\n<li><strong>\u8fdb\u884c\u53c2\u6570\u9a8c\u8bc1\uff1a<\/strong>\u5728\u51fd\u6570\u4f53\u4e2d\uff0c\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6216\u5176\u4ed6\u9a8c\u8bc1\u673a\u5236\u68c0\u67e5\u53c2\u6570\u7684\u6709\u6548\u6027\u3002<\/li>\n<li><strong>\u8fd4\u56de\u9519\u8bef\uff1a<\/strong>\u5982\u679c\u68c0\u6d4b\u5230\u65e0\u6548\u53c2\u6570\uff0c\u8fd4\u56de\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/ \u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\ntype InvalidArgumentError struct {\n    message string\n}\nfunc (e InvalidArgumentError) Error() string {\n    return e.message\n}\n\n\/\/ \u5177\u6709\u53c2\u6570\u9a8c\u8bc1\u7684\u51fd\u6570\nfunc MyFunction(arg1 string, arg2 int) error {\n    if arg1 == \"\" {\n        return &amp;InvalidArgumentError{\n            message: \"arg1 cannot be empty\",\n        }\n    }\n\n    if arg2 &lt; 0 {\n        return &amp;InvalidArgumentError{\n            message: \"arg2 cannot be\u8d1f\u6570\",\n        }\n    }\n\n    \/\/ \u53c2\u6570\u6709\u6548\uff0c\u7ee7\u7eed\u6267\u884c\u51fd\u6570\u903b\u8f91...\n    return nil\n}<\/pre>\n<h2>\u5b9e\u6218\u6848\u4f8b<\/h2>\n<p>\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u5e94\u7528\u7a0b\u5e8f\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\u6765\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"log\"\n\n    \"errors\"\n)\n\nfunc main() {\n    err := MyFunction(\"\", 10)\n    if err != nil {\n        log.Fatal(err)\n    }\n}\n\nfunc MyFunction(arg1 string, arg2 int) error {\n    if arg1 == \"\" {\n        return errors.New(\"arg1 cannot be empty\")\n    }\n\n    if arg2 &lt; 0 {\n        return errors.New(\"arg2 cannot be negative\")\n    }\n\n    fmt.Printf(\"arg1=%q, arg2=%d\", arg1, arg2)\n    return nil\n}<\/pre>\n<p>\u8fd0\u884c\u6b64\u5e94\u7528\u7a0b\u5e8f\u5c06\u5bfc\u81f4\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n<pre>arg1=\"\", arg2=10<\/pre>\n<p>\u5728\u8be5\u6848\u4f8b\u4e2d\uff0c\u51fd\u6570 MyFunction \u4f7f\u7528 <code>errors.New<\/code> \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u9519\u8bef\u5bf9\u8c61\uff0c\u5b83\u5305\u542b\u4e00\u4e2a\u8bf4\u660e\u65e0\u6548\u53c2\u6570\u95ee\u9898\u7684\u9519\u8bef\u6d88\u606f\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5728 GOLANG \u51fd\u6570\u4e2d\u5982\u4f55\u5904\u7406\u9519\u8bef\u7684\u53c2\u6570\u8f93\u5165\uff1f\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\u6ce8GOLANG\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5728 CSS \u52a8\u753b\u4e2d\u5982\u4f55\u5b9e\u73b0\u5bf9\u8c61\u8df3\u8dc3\u5230\u6307\u5b9a\u4f4d\u7f6e\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u5728\u7eaf CSS \u4e2d\u521b\u5efa\u6d41\u5f0f\u5e03\u5c40\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5728 GOLA&#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-205909","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205909","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=205909"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205909\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}