{"id":208084,"date":"2025-07-08T08:48:07","date_gmt":"2025-07-08T00:48:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208084\/"},"modified":"2025-07-08T08:48:07","modified_gmt":"2025-07-08T00:48:07","slug":"%e4%bb%8ego%e7%bb%93%e6%9e%84%e4%b8%ad%e6%8f%90%e5%8f%96%e6%a0%87%e7%ad%be%e4%bd%9c%e4%b8%bareflect-value","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208084\/","title":{"rendered":"\u4eceGo\u7ed3\u6784\u4e2d\u63d0\u53d6\u6807\u7b7e\u4f5c\u4e3areflect.Value"},"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>\u4eceGo\u7ed3\u6784\u4e2d\u63d0\u53d6\u6807\u7b7e\u4f5c\u4e3areflect.Value<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-28 20:36:35<\/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: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4eceGo\u7ed3\u6784\u4e2d\u63d0\u53d6\u6807\u7b7e\u4f5c\u4e3areflect.Value\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u4ece\u67d0\u4e2a\u7ed3\u6784\u4f53\u503c\u4e2d\u63d0\u53d6\u6807\u7b7e\u3002\u6211\u80fd\u591f\u83b7\u53d6\u7ed3\u6784\u7684\u5b57\u6bb5\uff0c\u4f46\u65e0\u6cd5\u63d0\u53d6\u6807\u7b7e\u3002\u6211\u5728\u8fd9\u91cc\u505a\u9519\u4e86\u4ec0\u4e48\uff1f\u6211\u5c1d\u8bd5\u4e86\u5f88\u591a\u4e0d\u540c\u7684\u65b9\u6cd5\uff08\u4f7f\u7528 reflect.type\u3001interface{} \u7b49\uff09\uff0c\u4f46\u90fd\u5931\u8d25\u4e86\u3002<\/p>\n<pre>type house struct {\n    room string\n    humans human\n}\n\ntype human struct {\n    name         string `anonymize:\"true\"` \/\/ unable to get this tag\n    body         string\n    tail         string `anonymize:\"true\"` \/\/ unable to get this tag\n}\n\nfunc printstructtags(f reflect.value) { \/\/ f is of struct type `human`\n    for i := 0; i &lt; f.numfield(); i++ {\n        fmt.printf(\"tags are %s\\n\", reflect.typeof(f).field(i).tag) \/\/ tags aren't printed\n    }\n}<\/pre>\n<p>\u6211\u4f7f\u7528 <code>reflect.value<\/code> \u4f5c\u4e3a\u53c2\u6570\u7684\u539f\u56e0\u662f\u56e0\u4e3a\u8be5\u51fd\u6570\u662f\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4ece\u53e6\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u7684<\/p>\n<pre>var payload interface{}\n    payload = &amp;House{}\n    \/\/ Setup complete\n    v := reflect.ValueOf(payload).Elem()\n    for j := 0; j &lt; v.NumField(); j++ { \/\/ Go through all fields of payload\n        f := v.Field(j)\n        if f.Kind().String() == \"struct\" { \n            printStructTags(f)\n        }\n    }<\/pre>\n<p>\u4efb\u4f55\u89c1\u89e3\u90fd\u975e\u5e38\u6709\u4ef7\u503c<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5f53\u4f60\u8c03\u7528 <code>reflect.typeof(f)<\/code> \u65f6\uff0c\u4f60\u4f1a\u5f97\u5230 <code>f<\/code> \u7684\u7c7b\u578b\uff0c\u5b83\u5df2\u7ecf\u662f <code>reflect.value<\/code> \u4e86\u3002<\/p>\n<p>\u4f7f\u7528\u6b64 <code>f<\/code> \u7684 <code>type()<\/code> \u51fd\u6570\u6765\u83b7\u53d6\u7c7b\u578b\u5e76\u5bf9\u5176\u8fdb\u884c\u5b57\u6bb5\u68c0\u67e5\uff1a<\/p>\n<pre>func printStructTags(f reflect.Value) { \/\/ f is of struct type `human`\n    for i := 0; i &lt; f.NumField(); i++ {\n        fmt.Printf(\"Tags are %s\\n\", f.Type().Field(i).Tag) \n    }\n}<\/pre>\n<p>\u4f46\u662f\uff0c\u4f7f\u7528 <code>f.type().field(i).tag.get(\"anonymize\")<\/code> \u68c0\u67e5\u6807\u7b7e\u5e76\u83b7\u53d6\u5176\u503c\u4f1a\u66f4\u5bb9\u6613\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u5206\u914d\u7684\u503c\uff0c\u4f8b\u5982\uff0c\u5728\u60a8\u7684\u60c5\u51b5\u4e0b\u4e3a <code>true<\/code> \uff0c\u5982\u679c\u6807\u7b7e\u4e0d\u5b58\u5728\uff0c\u5219\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u4eceGo\u7ed3\u6784\u4e2d\u63d0\u53d6\u6807\u7b7e\u4f5c\u4e3areflect.Value\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-208084","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208084","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=208084"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208084\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}