{"id":207803,"date":"2025-07-08T08:13:24","date_gmt":"2025-07-08T00:13:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207803\/"},"modified":"2025-07-08T08:13:24","modified_gmt":"2025-07-08T00:13:24","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e5%b5%8c%e5%a5%97%e7%bb%93%e6%9e%84%e4%b8%ad%e7%9a%84%e5%ad%97%e6%ae%b5%e8%ae%be%e7%bd%ae%e4%b8%ba%e9%9b%b6%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207803\/","title":{"rendered":"\u5982\u4f55\u5c06\u5d4c\u5957\u7ed3\u6784\u4e2d\u7684\u5b57\u6bb5\u8bbe\u7f6e\u4e3a\u96f6\u503c\uff1f"},"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>\u5982\u4f55\u5c06\u5d4c\u5957\u7ed3\u6784\u4e2d\u7684\u5b57\u6bb5\u8bbe\u7f6e\u4e3a\u96f6\u503c\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 18:30:37<\/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=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5c06\u5d4c\u5957\u7ed3\u6784\u4e2d\u7684\u5b57\u6bb5\u8bbe\u7f6e\u4e3a\u96f6\u503c\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5047\u8bbe\u6211\u6709\u4e00\u4e2a struct <code>thing1<\/code> \u5b9e\u4f8b\uff0c\u6211\u60f3\u8981 <code>json.marshal<\/code><\/p>\n<pre>type thing1 struct {\n    a string `json:\"a,omitempty\"`\n    b int    `json:\"b,omitempty\"`\n    c thing2 `json:\"c,omitempty\"`\n}\n\ntype thing2 struct {\n    d bool `json:\"d,omitempty\"`\n    e int  `json:\"e,omitempty\"`\n}\n\n...\n\nthing1 := thing1{\n    a: \"test\",\n    b: 42,\n    c: thing2{d: true, e: 43},\n}<\/pre>\n<p>\u60a8\u5c06\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u91c7\u7528\u4efb\u4f55\u7ed3\u6784\u4f53\u7684\u5b9e\u4f8b\u548c\u8981\u7f16\u8f91\u7684\u5b57\u6bb5\u5217\u8868\uff0c\u5e76\u8fd4\u56de\u4f20\u5165\u5bf9\u8c61\u7684\u514b\u9686\uff08\u6216\u53ea\u662f\u53d8\u5f02\uff09\uff0c\u4f46\u5c06\u7f16\u8f91\u7684\u5b57\u6bb5\u8bbe\u7f6e\u4e3a\u96f6\u503c\uff1f p&gt; <\/p>\n<pre>redact(thing1, []string{\"B\", \"D\"})\nthing1 == Thing1{\n    A: \"test\",\n    B: 0,\n    C: Thing2{D: false, E: 43},\n}<\/pre>\n<p>\u6211\u65e0\u6cd5\u4f7f\u7528 <code>json:\"-\"<\/code> \u4f5c\u4e3a\u5b57\u6bb5\u6807\u8bb0\uff0c\u56e0\u4e3a\u6211\u6b63\u5728\u4f7f\u7528\u7684\u67e5\u8be2\u8bed\u8a00 (dgraph) \u9700\u8981\u5f53\u524d\u7684\u5b57\u6bb5\u6807\u8bb0\u3002<\/p>\n<p>\u7f16\u8f91\uff1a\u4e0d\u5728\u793a\u4f8b\u4e2d\uff0c\u4f46\u5982\u679c\u9002\u7528\uff0c\u4e5f\u5e94\u7f16\u8f91\u6570\u7ec4\u5185\u7684\u5bf9\u8c61<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u4f7f\u7528reflect\u6765\u64cd\u4f5cstruct\u5b57\u6bb5\u7684\u503c\u3002\u4ee5\u4e0b\u662f\u6211\u5728\u8bc4\u8bba\u4e2d\u6240\u5199\u5185\u5bb9\u7684\u6982\u5ff5\u8bc1\u660e\u3002\u7531\u4e8e\u8fd9\u53ea\u662f\u4e00\u4e2a poc\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8c03\u6574\/\u4fee\u6539\u4ee3\u7801\u4ee5\u6ee1\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n<p>\u8be5\u51fd\u6570\u4f1a\u6539\u53d8\u539f\u59cb\u6570\u636e\u3002\u4ee3\u7801\u662f\u4e0d\u8a00\u81ea\u660e\u7684\u3002<\/p>\n<pre>func redact(target interface{}, fieldstomodify []string) {\n    \/\/ if target is not pointer, then immediately return\n    \/\/ modifying struct's field requires addresable object\n    addrvalue := reflect.valueof(target)\n    if addrvalue.kind() != reflect.ptr {\n        return\n    }\n\n    \/\/ if target is not struct then immediatelly return\n    \/\/ this might need to be modified as per your needs\n    targetvalue := addrvalue.elem()\n    targettype := targetvalue.type()\n    if targettype.kind() != reflect.struct {\n        return\n    }\n\n    \/\/ loop the fields\n    for i := 0; i &lt; targettype.numfield(); i++ {\n        ftype := targettype.field(i)\n        fvalue := targetvalue.field(i)\n\n        \/\/ if the field type is struct, then call redact() recursively\n        if fvalue.kind() == reflect.struct {\n            redact(fvalue.addr().interface(), fieldstomodify)\n            continue\n        } \n\n        \/\/ if the field is slice, loop then call redact() recursively\n        if fvalue.kind() == reflect.array || fvalue.kind() == reflect.slice {\n            for i := 0; i &lt; fvalue.len(); i++ {\n                redact(fvalue.index(i).addr().interface(), fieldstomodify)\n            }\n            continue\n        }\n\n        \/\/ loop the fieldstomodify\n        for _, fieldtomodify := range fieldstomodify {\n            if fieldtomodify == ftype.name &amp;&amp; fvalue.canset() {\n                fvalue.set(reflect.zero(ftype.type))\n            }\n        }\n    }\n}<\/pre>\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e2d\u7684 <code>redact()<\/code> \u51fd\u6570\u6307\u9488\u6570\u636e\uff0c\u56e0\u4e3a\u4fee\u6539\u5b57\u6bb5\u9700\u8981\u53ef\u5bfb\u5740\u5bf9\u8c61\u3002<\/p>\n<pre>type Thing2 struct {\n    D bool `json:\"d,omitempty\"`\n    E int  `json:\"e,omitempty\"`\n}\n\ntype Thing1 struct {\n    A string   `json:\"a,omitempty\"`\n    B int      `json:\"b,omitempty\"`\n    C Thing2   `json:\"c,omitempty\"`\n    H []Thing2 `json:\"h,omitempty\"`\n}\n\nthing1 := Thing1{\n    A: \"test\",\n    B: 42,\n    C: Thing2{D: true, E: 43},\n    H: []Thing2{Thing2{D: true, E: 43}},\n}\n\nfmt.Printf(\"before: %#v \\n\", thing1)\n\/\/ before: main.Thing1{A:\"test\", B:42, C:main.Thing2{D:true, E:43}, H:[]main.Thing2{main.Thing2{D:true, E:43}}} \n\nredact(&amp;thing1, []string{\"B\", \"D\"})\nfmt.Printf(\"after: %#v \\n\", thing1)\n\/\/ after: main.Thing1{A:\"test\", B:0, C:main.Thing2{D:false, E:43}, H:[]main.Thing2{main.Thing2{D:false, E:43}}}<\/pre>\n<p>\u6f14\u793a\uff1a<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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-207803","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207803","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=207803"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207803\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}