{"id":207998,"date":"2025-07-08T10:13:01","date_gmt":"2025-07-08T02:13:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207998\/"},"modified":"2025-07-08T10:13:01","modified_gmt":"2025-07-08T02:13:01","slug":"golang-%e9%aa%8c%e8%af%81%e5%99%a8%e8%87%aa%e5%ae%9a%e4%b9%89%e6%9e%9a%e4%b8%be%e9%aa%8c%e8%af%81%e8%a7%84%e5%88%99","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207998\/","title":{"rendered":"Golang \u9a8c\u8bc1\u5668\u81ea\u5b9a\u4e49\u679a\u4e3e\u9a8c\u8bc1\u89c4\u5219"},"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>Golang \u9a8c\u8bc1\u5668\u81ea\u5b9a\u4e49\u679a\u4e3e\u9a8c\u8bc1\u89c4\u5219<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 19:15: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><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGolang \u9a8c\u8bc1\u5668\u81ea\u5b9a\u4e49\u679a\u4e3e\u9a8c\u8bc1\u89c4\u5219\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\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u4f7f\u7528 https:\/\/github.com\/go-playground\/validator\uff0c\u6211\u9700\u8981\u4e3a\u4e0d\u540c\u7684\u679a\u4e3e\u503c\u521b\u5efa\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u89c4\u5219\u3002\u8fd9\u662f\u6211\u7684\u7ed3\u6784 &#8211; https:\/\/go.dev\/play\/p\/UmR6YH6cvK9\u3002\u5982\u60a8\u6240\u89c1\uff0c\u6211\u6709 3 \u79cd\u4e0d\u540c\u7684\u7528\u6237\u7c7b\u578b\uff1a\u7ba1\u7406\u5458\u3001\u7248\u4e3b\u548c\u5185\u5bb9\u521b\u5efa\u8005\uff0c\u6211\u60f3\u4e3a\u4ed6\u4eec\u8c03\u6574\u4e0d\u540c\u7684\u5bc6\u7801\u89c4\u5219\u3002\u4f8b\u5982\uff0c\u7ba1\u7406\u5458\u7684\u5bc6\u7801\u957f\u5ea6\u5e94\u81f3\u5c11\u4e3a 7 \u4e2a\u7b26\u53f7\uff0c\u800c\u7248\u4e3b\u7684\u5bc6\u7801\u957f\u5ea6\u5e94\u81f3\u5c11\u4e3a 5 \u4e2a\u3002\u662f\u5426\u53ef\u4ee5\u901a\u8fc7 go-playground\/validator \u4e2d\u7684\u6807\u7b7e\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff1f<\/p>\n<p>\u6211\u7684\u670d\u52a1\u83b7\u53d6\u7528\u6237\u5217\u8868\uff0c\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u89c4\u5219\u8fdb\u884c\u9a8c\u8bc1<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u5411 <code>usertype<\/code> \u6dfb\u52a0\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4f7f\u7528 <code>validator<\/code> \u5305\u6765\u9a8c\u8bc1\u7528\u6237\u3002<\/p>\n<pre>type usertype int\n\nconst (\n    admin usertype = iota\n    moderator\n    contentcreator\n)\n\nfunc (u usertype) validate() error {\n    switch u {\n    case admin:\n        \/\/ validate admin\n    case moderator:\n        \/\/ validate moderator\n    case contentcreator:\n        \/\/ validate content creator\n    default:\n        return fmt.errorf(\"invalid user type\")\n    }\n    return nil\n}\n<\/pre>\n<p>\u8c03\u7528 validate \u770b\u8d77\u6765\u50cf\u8fd9\u6837<\/p>\n<pre>func main() {\n    a := User{\n        Type:         Admin,\n        Name:         \"admin\",\n        Password:     \"pass\",\n        LastActivity: time.Time{},\n    }\n\n    err := a.Type.Validate()\n    if err != nil {\n        fmt.Println(\"invalid user: %w\", err)\n    }\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/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-207998","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207998","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=207998"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207998\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}