{"id":206623,"date":"2025-06-12T08:10:40","date_gmt":"2025-06-12T00:10:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206623\/"},"modified":"2025-06-12T08:10:40","modified_gmt":"2025-06-12T00:10:40","slug":"simplifying-string-validation-in-go-introducing-validatorgo","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206623\/","title":{"rendered":"Simplifying String Validation in Go: Introducing validatorgo"},"content":{"rendered":"<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230GOLANG\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aSimplifying String Validation in Go: Introducing validatorgo\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<br \/>\n<br \/>,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241117\/1731826671673993ef1de61.jpg\" class=\"aligncenter\">,\u5b57\u7b26\u4e32\u9a8c\u8bc1\u5668\u548c\u6e05\u7406\u5668\u7684\u5e93\uff0c\u57fa\u4e8e js \u5e93 validator.js,\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528\u6d41\u884c\u7684 go \u5e93\uff0c\u5982 package validator \u6216 govalidator\uff1f\u867d\u7136\u8fd9\u4e24\u4e2a\u5e93\u90fd\u5f88\u51fa\u540d\uff0c\u4f46<br \/>\n<strong>validatorgo<\/strong> \u4e13\u6ce8\u4e8e\u72ec\u7acb\u5b57\u7b26\u4e32\u9a8c\u8bc1\uff0c\u5e76\u63d0\u4f9b\u4e86\u53d7 validator.js \u542f\u53d1\u7684\u5e7f\u6cdb\u7684\u53ef\u5b9a\u5236\u9a8c\u8bc1\u5668\u96c6\u5408\uff0c\u800c\u8fd9\u4e24\u4e2a go \u5e93\u90fd\u6ca1\u6709\u5b8c\u5168\u5b9e\u73b0\u3002,\u4ee5\u4e0b\u662f<br \/>\n<strong>validatorgo<\/strong> \u4e0e go-playground\/validator \u548c govalidator \u76f8\u6bd4\u7684\u7a81\u51fa\u4e4b\u5904\uff1a,\u6211\u521b\u5efa\u4e86<br \/>\n<strong>validatorgo<\/strong> \u4f5c\u4e3a\u53e6\u4e00\u4e2a\u540d\u4e3a ginvalidator \u7684 go \u5e93\u7684\u4f9d\u8d56\u9879\uff0c\u8be5\u5e93\u9a8c\u8bc1 go web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684 http \u8bf7\u6c42\u3002\u53d7 express-validator\uff08node.js \u548c express \u7684\u6d41\u884c\u9a8c\u8bc1\u5e93\uff09\u7684\u542f\u53d1\uff0c<br \/>\n<strong>validatorgo<\/strong> \u586b\u8865\u4e86 go \u751f\u6001\u7cfb\u7edf\u4e2d\u7684\u7a7a\u767d\uff0c\u5b9e\u73b0\u9ad8\u6548\u3001\u53ef\u5b9a\u5236\u4e14\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u9a8c\u8bc1\u3002\u7531\u4e8e\u5176\u4ed6\u5e93\u8981\u4e48\u77eb\u6789\u8fc7\u6b63\uff0c\u7f3a\u4e4f\u529f\u80fd\uff0c\u8981\u4e48\u4e0d\u6ee1\u8db3\u6211\u7684\u7528\u4f8b\uff0c\u6211\u6784\u5efa\u4e86<br \/>\n<strong>validatorgo<\/strong> \u6765\u63d0\u4f9b\u5b9e\u7528\u7684\u89e3\u51b3\u65b9\u6848\u3002,\u4f7f\u7528 go get\u3002<br \/>\n<br \/>,\u7136\u540e\u5c06\u5305\u5bfc\u5165\u5230\u60a8\u81ea\u5df1\u7684\u4ee3\u7801\u4e2d\u3002<br \/>\n<br \/>,\u5982\u679c\u60a8\u4e0d\u6ee1\u610f\u4f7f\u7528\u957f validatorgo \u5305\u540d\u79f0\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u505a\u3002<br \/>\n<br \/>,\u4e0b\u9762\u662f validatorgo \u5305\u63d0\u4f9b\u7684\u9a8c\u8bc1\u5668\u5217\u8868\uff0c\u6db5\u76d6\u4e86\u5404\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u548c\u7c7b\u578b\uff0c\u4f7f\u5176\u80fd\u591f\u6ee1\u8db3\u591a\u79cd\u9a8c\u8bc1\u9700\u6c42\u3002,\u8be5\u8868\u5e94\u8be5\u6db5\u76d6 validatorgo \u4e2d\u5f53\u524d\u53ef\u7528\u7684\u5927\u591a\u6570\u9a8c\u8bc1\u5668\u3002\u8bf7\u52a1\u5fc5\u53c2\u9605\u5305\u7684\u6587\u6863\u4ee5\u4e86\u89e3\u6bcf\u4e2a\u9a8c\u8bc1\u5668\u7684\u66f4\u8be6\u7ec6\u7528\u6cd5\u3002,\u793a\u4f8b<br \/>\n<br \/>,\u8fd9\u4e9b\u6e05\u7406\u7a0b\u5e8f\u901a\u5e38\u7528\u4e8e\u901a\u8fc7\u5220\u9664\u6216\u4fee\u6539\u6f5c\u5728\u4e0d\u9700\u8981\u6216\u5371\u9669\u7684\u5b57\u7b26\u6765\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\u548c\u5b89\u5168\u6027\u3002,\u8bf7\u52a1\u5fc5\u53c2\u8003 validatorgo \u5b98\u65b9\u6587\u6863\uff0c\u4e86\u89e3\u6bcf\u79cd\u6d88\u6bd2\u5242\u7684\u5177\u4f53\u5b9e\u73b0\u548c\u793a\u4f8b\u3002,<br \/>\n<strong>validatorgo<\/strong> \u5982\u679c\u60a8\u9700\u8981\u7684\u8bdd\uff0c\u662f\u7406\u60f3\u7684\u9009\u62e9\uff1a,\u4f7f\u7528<br \/>\n<strong>validatorgo<\/strong>\uff0c\u60a8\u5c06\u83b7\u5f97\u4e13\u95e8\u4e3a\u5b57\u7b26\u4e32\u9a8c\u8bc1\u800c\u8bbe\u8ba1\u7684\u5de5\u5177\uff0c\u652f\u6301 go \u4e2d\u7684\u72ec\u7acb\u548c web \u5e94\u7528\u7a0b\u5e8f\u8981\u6c42\u3002,\u4eca\u5929\u5173\u4e8e\u300aSimplifying String Validation in Go: Introducing validatorgo\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728GOLANG\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01,<br \/>\n<strong>\u5b9a\u5236\u548c\u7075\u6d3b\u6027<\/strong>\uff1agovalidator \u4e3a\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u9a8c\u8bc1\u5668\uff0c\u4f46\u662f<br \/>\n<strong>validatorgo<\/strong> \u901a\u8fc7\u5141\u8bb8\u5355\u4e2a\u9a8c\u8bc1\u5668\u7684\u7279\u5b9a\u9009\u9879\u548c\u914d\u7f6e\u6765\u589e\u5f3a\u7075\u6d3b\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u65e5\u671f\u683c\u5f0f\u6216\u533a\u57df\u8bbe\u7f6e\u89c4\u8303\uff0c\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u66f4\u597d\u5730\u63a7\u5236\u6839\u636e\u9879\u76ee\u9700\u6c42\u5b9a\u5236\u7684\u9a8c\u8bc1\u89c4\u5219\u3002,<\/p>\n<p><strong>\u6027\u80fd<\/strong>\uff1ago-playground\/validator \u4f9d\u8d56\u53cd\u5c04\u6765\u52a8\u6001\u68c0\u67e5\u7ed3\u6784\u6807\u7b7e\u3002\u53cd\u5c04\u867d\u7136\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u4f1a\u5e26\u6765\u6027\u80fd\u5f00\u9500\uff0c\u5c24\u5176\u662f\u5728\u9a8c\u8bc1\u5927\u578b\u6216\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u3002 <strong>validatorgo<\/strong> \u907f\u514d\u4e86\u53cd\u5c04\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u6027\u80fd\uff0c\u5bf9\u4e8e\u9700\u8981\u5355\u5b57\u6bb5\u9a8c\u8bc1\u7684\u573a\u666f\u6765\u8bf4\uff0c\u901f\u5ea6\u66f4\u5feb\u3001\u6548\u7387\u66f4\u9ad8\u3002<\/p>\n<p>,<\/p>\n<p><strong>\u76f4\u63a5\u5b57\u7b26\u4e32\u9a8c\u8bc1<\/strong>\uff1ago-playground\/validator \u4e3b\u8981\u7528\u4e8e\u4f7f\u7528\u6807\u7b7e\u9a8c\u8bc1\u7ed3\u6784\u5b57\u6bb5\uff0c\u8fd9\u975e\u5e38\u9002\u5408\u5904\u7406 json \u6216\u57fa\u4e8e\u7ed3\u6784\u7684\u6570\u636e\u3002\u7136\u800c\uff0c\u5b83\u5e76\u4e0d\u662f\u4e3a\u9a8c\u8bc1\u5355\u4e2a\u5b57\u7b26\u4e32\u800c\u8bbe\u8ba1\u7684\uff0c<strong>validatorgo<\/strong> \u53ef\u4ee5\u65e0\u7f1d\u5730\u9a8c\u8bc1\u5355\u4e2a\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u9700\u8981\u7ed3\u6784\u6807\u7b7e\u6216\u989d\u5916\u7684\u8bbe\u7f6e\u3002<\/p>\n<p>,\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230GOLANG\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aSimplifying String Validation in Go: Introducing validatorgo\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<br \/>\n<br \/>,<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>Simplifying String Validation in Go: Introducing validatorgo<\/span><\/p>\n<h1>Simplifying String Validation in Go: Introducing validatorgo<\/h1>\n<p><span>\u6765\u6e90\uff1adev.to<\/span><br \/>\n<span>2024-11-17 14:58:06<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230GOLANG\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aSimplifying String Validation in Go: Introducing validatorgo\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241117\/1731826671673993ef1de61.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5b57\u7b26\u4e32\u9a8c\u8bc1\u5668\u548c\u6e05\u7406\u5668\u7684\u5e93\uff0c\u57fa\u4e8e js \u5e93 validator.js<\/p>\n<h2> \u4e3a\u4ec0\u4e48\u9009\u62e9\u9a8c\u8bc1\u5668go\uff1f <\/h2>\n<p>\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528\u6d41\u884c\u7684 go \u5e93\uff0c\u5982 package validator \u6216 govalidator\uff1f\u867d\u7136\u8fd9\u4e24\u4e2a\u5e93\u90fd\u5f88\u51fa\u540d\uff0c\u4f46 <strong>validatorgo<\/strong> \u4e13\u6ce8\u4e8e\u72ec\u7acb\u5b57\u7b26\u4e32\u9a8c\u8bc1\uff0c\u5e76\u63d0\u4f9b\u4e86\u53d7 validator.js \u542f\u53d1\u7684\u5e7f\u6cdb\u7684\u53ef\u5b9a\u5236\u9a8c\u8bc1\u5668\u96c6\u5408\uff0c\u800c\u8fd9\u4e24\u4e2a go \u5e93\u90fd\u6ca1\u6709\u5b8c\u5168\u5b9e\u73b0\u3002 <\/p>\n<p>\u4ee5\u4e0b\u662f <strong>validatorgo<\/strong> \u4e0e go-playground\/validator \u548c govalidator \u76f8\u6bd4\u7684\u7a81\u51fa\u4e4b\u5904\uff1a<\/p>\n<hr>\n<h3> 1. \u4e0e go-playground\/validator \u76f8\u6bd4 <\/h3>\n<ul>\n<li>\n<p><strong>\u76f4\u63a5\u5b57\u7b26\u4e32\u9a8c\u8bc1<\/strong>\uff1ago-playground\/validator \u4e3b\u8981\u7528\u4e8e\u4f7f\u7528\u6807\u7b7e\u9a8c\u8bc1\u7ed3\u6784\u5b57\u6bb5\uff0c\u8fd9\u975e\u5e38\u9002\u5408\u5904\u7406 json \u6216\u57fa\u4e8e\u7ed3\u6784\u7684\u6570\u636e\u3002\u7136\u800c\uff0c\u5b83\u5e76\u4e0d\u662f\u4e3a\u9a8c\u8bc1\u5355\u4e2a\u5b57\u7b26\u4e32\u800c\u8bbe\u8ba1\u7684\uff0c<strong>validatorgo<\/strong> \u53ef\u4ee5\u65e0\u7f1d\u5730\u9a8c\u8bc1\u5355\u4e2a\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u9700\u8981\u7ed3\u6784\u6807\u7b7e\u6216\u989d\u5916\u7684\u8bbe\u7f6e\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd<\/strong>\uff1ago-playground\/validator \u4f9d\u8d56\u53cd\u5c04\u6765\u52a8\u6001\u68c0\u67e5\u7ed3\u6784\u6807\u7b7e\u3002\u53cd\u5c04\u867d\u7136\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u4f1a\u5e26\u6765\u6027\u80fd\u5f00\u9500\uff0c\u5c24\u5176\u662f\u5728\u9a8c\u8bc1\u5927\u578b\u6216\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u3002 <strong>validatorgo<\/strong> \u907f\u514d\u4e86\u53cd\u5c04\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u6027\u80fd\uff0c\u5bf9\u4e8e\u9700\u8981\u5355\u5b57\u6bb5\u9a8c\u8bc1\u7684\u573a\u666f\u6765\u8bf4\uff0c\u901f\u5ea6\u66f4\u5feb\u3001\u6548\u7387\u66f4\u9ad8\u3002<\/p>\n<\/li>\n<\/ul>\n<hr>\n<h3> 2. \u4e0e asasskevich\/govalidator \u76f8\u6bd4 <\/h3>\n<ul>\n<li> <strong>\u5b9a\u5236\u548c\u7075\u6d3b\u6027<\/strong>\uff1agovalidator \u4e3a\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u9a8c\u8bc1\u5668\uff0c\u4f46\u662f <strong>validatorgo<\/strong> \u901a\u8fc7\u5141\u8bb8\u5355\u4e2a\u9a8c\u8bc1\u5668\u7684\u7279\u5b9a\u9009\u9879\u548c\u914d\u7f6e\u6765\u589e\u5f3a\u7075\u6d3b\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u65e5\u671f\u683c\u5f0f\u6216\u533a\u57df\u8bbe\u7f6e\u89c4\u8303\uff0c\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u66f4\u597d\u5730\u63a7\u5236\u6839\u636e\u9879\u76ee\u9700\u6c42\u5b9a\u5236\u7684\u9a8c\u8bc1\u89c4\u5219\u3002<\/li>\n<\/ul>\n<h3> \u9879\u76ee\u52a8\u673a <\/h3>\n<p>\u6211\u521b\u5efa\u4e86 <strong>validatorgo<\/strong> \u4f5c\u4e3a\u53e6\u4e00\u4e2a\u540d\u4e3a ginvalidator \u7684 go \u5e93\u7684\u4f9d\u8d56\u9879\uff0c\u8be5\u5e93\u9a8c\u8bc1 go web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684 http \u8bf7\u6c42\u3002\u53d7 express-validator\uff08node.js \u548c express \u7684\u6d41\u884c\u9a8c\u8bc1\u5e93\uff09\u7684\u542f\u53d1\uff0c<strong>validatorgo<\/strong> \u586b\u8865\u4e86 go \u751f\u6001\u7cfb\u7edf\u4e2d\u7684\u7a7a\u767d\uff0c\u5b9e\u73b0\u9ad8\u6548\u3001\u53ef\u5b9a\u5236\u4e14\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u9a8c\u8bc1\u3002\u7531\u4e8e\u5176\u4ed6\u5e93\u8981\u4e48\u77eb\u6789\u8fc7\u6b63\uff0c\u7f3a\u4e4f\u529f\u80fd\uff0c\u8981\u4e48\u4e0d\u6ee1\u8db3\u6211\u7684\u7528\u4f8b\uff0c\u6211\u6784\u5efa\u4e86 <strong>validatorgo<\/strong> \u6765\u63d0\u4f9b\u5b9e\u7528\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<h2> \u5b89\u88c5 <\/h2>\n<p>\u4f7f\u7528 go get\u3002<\/p>\n<pre> go get github.com\/bube054\/validatorgo\n<\/pre>\n<p>\u7136\u540e\u5c06\u5305\u5bfc\u5165\u5230\u60a8\u81ea\u5df1\u7684\u4ee3\u7801\u4e2d\u3002<\/p>\n<pre> import (\n   \"fmt\"\n   \"github.com\/bube054\/validatorgo\"\n )\n<\/pre>\n<p>\u5982\u679c\u60a8\u4e0d\u6ee1\u610f\u4f7f\u7528\u957f validatorgo \u5305\u540d\u79f0\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u505a\u3002<\/p>\n<pre> import (\n   \"fmt\"\n   vgo \"github.com\/bube054\/validatorgo\"\n )\n<\/pre>\n<h2> \u7b80\u5355\u7684\u9a8c\u8bc1\u5668\u793a\u4f8b <\/h2>\n<pre> func main(){\n   id := \"5f2a6c69e1d7a4e0077b4e6b\"\n   validid := vgo.ismongoid(id)\n   fmt.println(validid) \/\/ true\n }\n<\/pre>\n<h2> \u4e00\u4e9b\u9a8c\u8bc1\u5668 <\/h2>\n<p>\u4e0b\u9762\u662f validatorgo \u5305\u63d0\u4f9b\u7684\u9a8c\u8bc1\u5668\u5217\u8868\uff0c\u6db5\u76d6\u4e86\u5404\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u548c\u7c7b\u578b\uff0c\u4f7f\u5176\u80fd\u591f\u6ee1\u8db3\u591a\u79cd\u9a8c\u8bc1\u9700\u6c42\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>validator<\/th>\n<th>description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>contains<\/td>\n<td>checks if a string contains a specified substring.<\/td>\n<\/tr>\n<tr>\n<td>equals<\/td>\n<td>validates if a string is exactly equal to a comparison string.<\/td>\n<\/tr>\n<tr>\n<td>isabarouting<\/td>\n<td>checks if the string is a valid aba routing number (us bank accounts).<\/td>\n<\/tr>\n<tr>\n<td>isafter<\/td>\n<td>validates if a date string is after a specified date.<\/td>\n<\/tr>\n<tr>\n<td>isalpha<\/td>\n<td>ensures the string contains only letters (a-za-z).<\/td>\n<\/tr>\n<tr>\n<td>isalphanumeric<\/td>\n<td>validates if a string contains only letters and numbers.<\/td>\n<\/tr>\n<tr>\n<td>isascii<\/td>\n<td>checks if the string contains only ascii characters.<\/td>\n<\/tr>\n<tr>\n<td>isbase32<\/td>\n<td>checks if the string is a valid base32 encoded value.<\/td>\n<\/tr>\n<tr>\n<td>isbase64<\/td>\n<td>validates if a string is in base64 encoding.<\/td>\n<\/tr>\n<tr>\n<td>isbefore<\/td>\n<td>ensures the date is before a specified date.<\/td>\n<\/tr>\n<tr>\n<td>isboolean<\/td>\n<td>checks if the string is either &#8220;true&#8221; or &#8220;false&#8221;.<\/td>\n<\/tr>\n<tr>\n<td>iscreditcard<\/td>\n<td>validates if the string is a valid credit card number.<\/td>\n<\/tr>\n<tr>\n<td>iscurrency<\/td>\n<td>checks if the string is a valid currency format.<\/td>\n<\/tr>\n<tr>\n<td>isdate<\/td>\n<td>validates if a string is a valid date.<\/td>\n<\/tr>\n<tr>\n<td>isdecimal<\/td>\n<td>ensures the string represents a valid decimal number.<\/td>\n<\/tr>\n<tr>\n<td>isemail<\/td>\n<td>checks if the string is a valid email address format.<\/td>\n<\/tr>\n<tr>\n<td>isempty<\/td>\n<td>validates if a string is empty.<\/td>\n<\/tr>\n<tr>\n<td>isfqdn<\/td>\n<td>checks if the string is a fully qualified domain name.<\/td>\n<\/tr>\n<tr>\n<td>isfloat<\/td>\n<td>ensures the string represents a floating-point number.<\/td>\n<\/tr>\n<tr>\n<td>ishexcolor<\/td>\n<td>validates if a string is a valid hex color (e.g., #ffffff).<\/td>\n<\/tr>\n<tr>\n<td>isip<\/td>\n<td>checks if the string is a valid ip address (ipv4 or ipv6).<\/td>\n<\/tr>\n<tr>\n<td>isiso8601<\/td>\n<td>validates if the string is in iso8601 date format.<\/td>\n<\/tr>\n<tr>\n<td>islength<\/td>\n<td>checks if the string\u2019s length is within a specified range.<\/td>\n<\/tr>\n<tr>\n<td>ismimetype<\/td>\n<td>validates if the string is a valid mime type.<\/td>\n<\/tr>\n<tr>\n<td>ismobilephone<\/td>\n<td>checks if the string is a valid mobile phone number for specified locales.<\/td>\n<\/tr>\n<tr>\n<td>ismongoid<\/td>\n<td>validates if the string is a valid mongodb objectid.<\/td>\n<\/tr>\n<tr>\n<td>isnumeric<\/td>\n<td>ensures the string contains only numeric characters.<\/td>\n<\/tr>\n<tr>\n<td>ispostalcode<\/td>\n<td>checks if the string is a valid postal code for specified locale.<\/td>\n<\/tr>\n<tr>\n<td>isrfc3339<\/td>\n<td>validates if the string is in rfc3339 date format.<\/td>\n<\/tr>\n<tr>\n<td>isslug<\/td>\n<td>checks if the string is url-friendly (only letters, numbers, and dashes).<\/td>\n<\/tr>\n<tr>\n<td>isstrongpassword<\/td>\n<td>ensures the string meets common password strength requirements.<\/td>\n<\/tr>\n<tr>\n<td>isurl<\/td>\n<td>validates if the string is a url.<\/td>\n<\/tr>\n<tr>\n<td>isuuid<\/td>\n<td>checks if the string is a valid uuid (versions 1-5).<\/td>\n<\/tr>\n<tr>\n<td>isuppercase<\/td>\n<td>ensures the string is all uppercase.<\/td>\n<\/tr>\n<tr>\n<td>isvat<\/td>\n<td>checks if the string is a valid vat number for specified countries.<\/td>\n<\/tr>\n<tr>\n<td>matches<\/td>\n<td>validates if the string matches a specified regular expression.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8be5\u8868\u5e94\u8be5\u6db5\u76d6 validatorgo \u4e2d\u5f53\u524d\u53ef\u7528\u7684\u5927\u591a\u6570\u9a8c\u8bc1\u5668\u3002\u8bf7\u52a1\u5fc5\u53c2\u9605\u5305\u7684\u6587\u6863\u4ee5\u4e86\u89e3\u6bcf\u4e2a\u9a8c\u8bc1\u5668\u7684\u66f4\u8be6\u7ec6\u7528\u6cd5\u3002<\/p>\n<blockquote>\n<p><strong> \u6ce8\u610f<\/strong><\/p>\n<p> \u5f53\u4f7f\u7528\u9700\u8981\u9009\u9879\u7ed3\u6784\uff08\u6307\u9488\u6216\u975e\u6307\u9488\uff09\u7684\u9a8c\u8bc1\u5668\u65f6\uff0c\u8bf7\u59cb\u7ec8\u663e\u5f0f\u5730\u4e3a\u6240\u6709\u7ed3\u6784\u5b57\u6bb5\u63d0\u4f9b\u503c\u3002<br \/> \u4e0e validator.js \u4e2d\u7f3a\u5c11\u7684\u5b57\u6bb5\u4f1a\u81ea\u52a8\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\u4e0d\u540c\uff0cgo \u4f7f\u7528\u4e25\u683c\u7c7b\u578b\u3002<br \/> \u8fd9\u610f\u5473\u7740\u5e03\u5c14\u503c\u7684\u7f3a\u5931\u503c\u9ed8\u8ba4\u4e3a false\uff0c\u6570\u5b57\u7c7b\u578b\u7684\u7f3a\u5931\u503c\u9ed8\u8ba4\u4e3a 0\uff0c\u7b49\u7b49\u3002<br \/> \u5982\u679c\u60a8\u4e60\u60ef\u4e86 javascript \u7248\u672c\uff0c\u4e0d\u6307\u5b9a\u6240\u6709\u5b57\u6bb5\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u884c\u4e3a\u3002<\/p>\n<\/blockquote>\n<p>\u793a\u4f8b<\/p>\n<pre>  \/\/ do this (using the default options specified in the docs)\n  ok := validatorgo.isfqdn(\"example\", nil)\n\n  \/\/ or this (explicitly setting all possible fields for the structs)\n  ok := validatorgo.isfqdn(\"example\", &amp;validatorgo.isfqdnopts{\n    requiretld: false,\n    allowunderscores: false,\n    allowtrailingdot: true,\n    allownumerictld: false,\n    ignoremaxlength: true\n  })\n\n  \/\/ but rarely this(not explicitly setting all possible fields)\n  ok := validatorgo.isfqdn(\"example\", &amp;validatorgo.isfqdnopts{ requiretld: false, })\n<\/pre>\n<h2> \u7b80\u5355\u7684\u6d88\u6bd2\u5242\u793a\u4f8b <\/h2>\n<pre>  import (\n   \"fmt\"\n   \"github.com\/bube054\/validatorgo\/sanitizer\"\n )\n\n func main(){\n   str := sanitizer.Whitelist(\"Hello123 World!\", \"a-zA-Z\")\n   fmt.Println(str) \/\/ \"HelloWorld\"\n }\n<\/pre>\n<h2> \u6d88\u6bd2\u5242 <\/h2>\n<table>\n<thead>\n<tr>\n<th>sanitizer<\/th>\n<th>description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>trim<\/td>\n<td>removes whitespace from both ends of the string.<\/td>\n<\/tr>\n<tr>\n<td>ltrim<\/td>\n<td>removes whitespace from the left side of the string.<\/td>\n<\/tr>\n<tr>\n<td>rtrim<\/td>\n<td>removes whitespace from the right side of the string.<\/td>\n<\/tr>\n<tr>\n<td>tolower<\/td>\n<td>converts the entire string to lowercase.<\/td>\n<\/tr>\n<tr>\n<td>toupper<\/td>\n<td>converts the entire string to uppercase.<\/td>\n<\/tr>\n<tr>\n<td>escape<\/td>\n<td>escapes html characters in the string to prevent injection attacks.<\/td>\n<\/tr>\n<tr>\n<td>unescape<\/td>\n<td>reverts escaped html characters back to normal characters.<\/td>\n<\/tr>\n<tr>\n<td>normalizeemail<\/td>\n<td>standardizes an email address, e.g., removing dots in gmail addresses.<\/td>\n<\/tr>\n<tr>\n<td>blacklist<\/td>\n<td>removes characters from the string that match specified characters or patterns.<\/td>\n<\/tr>\n<tr>\n<td>whitelist<\/td>\n<td>retains only characters in the string that match specified characters or patterns.<\/td>\n<\/tr>\n<tr>\n<td>replace<\/td>\n<td>replaces occurrences of a substring with a specified replacement.<\/td>\n<\/tr>\n<tr>\n<td>striplow<\/td>\n<td>removes control characters, optionally allowing some specified ones.<\/td>\n<\/tr>\n<tr>\n<td>trimspace<\/td>\n<td>trims all types of whitespace from both ends of the string.<\/td>\n<\/tr>\n<tr>\n<td>toboolean<\/td>\n<td>converts common truthy and falsy values in strings into boolean true or false.<\/td>\n<\/tr>\n<tr>\n<td>toint<\/td>\n<td>converts a numeric string into an integer, if possible.<\/td>\n<\/tr>\n<tr>\n<td>tofloat<\/td>\n<td>converts a numeric string into a floating-point number, if possible.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u4e9b\u6e05\u7406\u7a0b\u5e8f\u901a\u5e38\u7528\u4e8e\u901a\u8fc7\u5220\u9664\u6216\u4fee\u6539\u6f5c\u5728\u4e0d\u9700\u8981\u6216\u5371\u9669\u7684\u5b57\u7b26\u6765\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<p>\u8bf7\u52a1\u5fc5\u53c2\u8003 validatorgo \u5b98\u65b9\u6587\u6863\uff0c\u4e86\u89e3\u6bcf\u79cd\u6d88\u6bd2\u5242\u7684\u5177\u4f53\u5b9e\u73b0\u548c\u793a\u4f8b\u3002<\/p>\n<h3> \u6982\u62ec <\/h3>\n<p><strong>validatorgo<\/strong> \u5982\u679c\u60a8\u9700\u8981\u7684\u8bdd\uff0c\u662f\u7406\u60f3\u7684\u9009\u62e9\uff1a<\/p>\n<ul>\n<li> <strong>\u9488\u5bf9\u5404\u4e2a\u5b57\u6bb5\u8fdb\u884c\u9ad8\u6548\u3001\u65e0\u53cd\u5c04\u7684\u9a8c\u8bc1<\/strong>\uff0c\u800c\u4e0d\u4f1a\u4ea7\u751f\u4e0e\u57fa\u4e8e\u7ed3\u6784\u7684\u53cd\u5c04\u76f8\u5173\u7684\u6027\u80fd\u6210\u672c\u3002<\/li>\n<li> <strong>\u9ad8\u5ea6\u53ef\u5b9a\u5236<\/strong>\u4e0e\u73b0\u4ee3\u6570\u636e\u683c\u5f0f\u4e00\u81f4\u7684\u9a8c\u8bc1\u9009\u9879\uff0c\u63d0\u4f9b\u4e0e validator.js \u76f8\u540c\u7684\u7a33\u5065\u6027\u3002<\/li>\n<\/ul>\n<p>\u4f7f\u7528 <strong>validatorgo<\/strong>\uff0c\u60a8\u5c06\u83b7\u5f97\u4e13\u95e8\u4e3a\u5b57\u7b26\u4e32\u9a8c\u8bc1\u800c\u8bbe\u8ba1\u7684\u5de5\u5177\uff0c\u652f\u6301 go \u4e2d\u7684\u72ec\u7acb\u548c web \u5e94\u7528\u7a0b\u5e8f\u8981\u6c42\u3002<\/p>\n<h2> \u7ef4\u62a4\u8005 <\/h2>\n<ul>\n<li> bube054 &#8211; attah gbubemi david\uff08\u4f5c\u8005\uff09<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300aSimplifying String Validation in Go: Introducing validatorgo\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728GOLANG\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Win7\u5982\u4f55\u4fee\u6539\u5207\u6362\u7a97\u53e3\u6570\u91cf? win7\u4fee\u6539\u5207\u6362\u7a97\u53e3\u6570\u91cf\u7684\u65b9\u6cd5\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   UC\u6d4f\u89c8\u5668\u7535\u8111\u7248\u600e\u4e48\u6837\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230GOLANG\uff0c&#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-206623","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206623","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=206623"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206623\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}