{"id":208241,"date":"2025-07-08T15:20:28","date_gmt":"2025-07-08T07:20:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208241\/"},"modified":"2025-07-08T15:20:28","modified_gmt":"2025-07-08T07:20:28","slug":"%e5%9c%a8%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e6%9f%a5%e6%89%be%e6%95%b0%e5%ad%97%e5%b9%b6%e5%b0%86%e5%85%b6%e6%9b%bf%e6%8d%a2%e4%b8%ba-golang-%e5%ad%97%e5%85%b8%e4%b8%ad%e7%9a%84%e5%ad%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208241\/","title":{"rendered":"\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u6570\u5b57\u5e76\u5c06\u5176\u66ff\u6362\u4e3a golang \u5b57\u5178\u4e2d\u7684\u5b57\u7b26\u4e32"},"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>\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u6570\u5b57\u5e76\u5c06\u5176\u66ff\u6362\u4e3a golang \u5b57\u5178\u4e2d\u7684\u5b57\u7b26\u4e32<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-30 17:09: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><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\">\u300a\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u6570\u5b57\u5e76\u5c06\u5176\u66ff\u6362\u4e3a golang \u5b57\u5178\u4e2d\u7684\u5b57\u7b26\u4e32\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\u662f golang \u65b0\u624b\uff1b\u4f46\u6211\u6709\u4e00\u4e2a\u9879\u76ee\u9700\u8981\u6211\u4f7f\u7528 golang\u3002<\/p>\n<p>\u6211\u7684\u95ee\u9898\uff1a\u6211\u6709\u4e00\u4e2a\u6765\u81ea\u53d8\u91cf\u7684\u8f93\u51fa\uff0c\u5e76\u4e14\u8be5\u8f93\u51fa\u5305\u542b\u4e00\u4e9b\u6570\u5b57\u3002\u8fd9\u4e9b\u6570\u5b57\u4ee3\u8868\u4e00\u4e2a\u547d\u540d\u5b9e\u4f8b\u3002<\/p>\n<p>\u6211\u60f3\u8981\u505a\u4ec0\u4e48\uff1a\u68c0\u67e5\u6570\u5b57\u7684\u8f93\u51fa\uff08alertmap\uff09\u5e76\u68c0\u67e5\u6839\u636e\u6211\u521b\u5efa\u7684\u5b57\u5178\u627e\u5230\u7684\u6570\u5b57\uff08num_var\uff09\u3002\u5982\u679c\u627e\u5230\u5339\u914d\u9879\uff1b\u5c06\u6570\u5b57 (num_var) \u66ff\u6362\u4e3a (alertmap) \u4e2d\u5b57\u5178\u4e2d\u7684\u5b57\u7b26\u4e32 (value)\u3002<\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u540d\u4e3aalertmap\u7684\u53d8\u91cf\uff0c\u5b83\u6709\u4e00\u4e2a\u952e\u548c\u4e00\u4e2a\u503c\u3002 \u5173\u952e\u662f\u6211\u8981\u6539\u4ec0\u4e48<\/p>\n<pre>package main\n\nimport (\n        \"bytes\"\n        \"context\"\n        \"encoding\/json\"\n        \"fmt\"\n        \"log\"\n        \"math\"\n        \"net\/http\"\n        \"os\"\n        \"strconv\"\n        \"time\"\n        \"errors\"\n        \"regexp\"\n        \"strings\"\n)\n\n\nmy_dicts = {\n123456: \"project_data\"\n2345671: \"project_dev-1\"\n}\n\nfunc alertme ()\n     for key, value := range alertmap\n           my_alert += fmt.Sprintln(\"\\n\", key, \"is a\", value.usage, \"and Expires in:\", value.expireComputed, \"days\")\n                }<\/pre>\n<p>\u6211\u60f3\u8981\u505a\u7684\u662f\u5728\u201cfor key, value := rangealertmap\u201d\u4e4b\u540e\u63d2\u5165\u4e00\u4e2a if statemnet \u6765\u68c0\u67e5\u952e\uff08\u4f8b\u5982 my\/123456\/secret\/salon\uff09\u4e2d\u662f\u5426\u6709\u4e00\u4e2a\u6570\u5b57\uff0c\u7136\u540e\u5c06\u8be5\u6570\u5b57\u66ff\u6362\u4e3a\u4ece\u5b57\u5178\u4e2d\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002\u5bc6\u94a5\u5c06\u662f my\/project_data\/secret\/salon \uff08\u8fd9\u662f\u6211\u60f3\u8981\u4f5c\u4e3a\u5bc6\u94a5\u53d1\u9001\u7684\u5185\u5bb9\uff09\u3002 \u4efb\u4f55\u5e2e\u52a9\u5c06\u4e0d\u80dc\u611f\u6fc0\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<pre>re := regexp.MustCompile(\"[0-9]+\")\n\nfunc alertme ()\n     for key, value := range alertmap {\n           my_alert += fmt.Sprintln(\"\\n\", key, \"is a\", value.usage, \"and Expires in:\", value.expireComputed, \"days\")\n                }\n           \/\/ Find value to replace and convert to int\n           dictKey, _ := strconv.Atoi(re.FindString(key))\n           \/\/ Replace value \n           replacedKey := re.ReplaceAllString(key, my_dicts[dictKey])\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-208241","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208241","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=208241"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208241\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}