{"id":206977,"date":"2025-07-08T08:15:45","date_gmt":"2025-07-08T00:15:45","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206977\/"},"modified":"2025-07-08T08:15:45","modified_gmt":"2025-07-08T00:15:45","slug":"%e5%9c%a8%e8%bf%90%e8%a1%8c%e6%97%b6%e6%af%94%e8%be%83%e5%92%8c%e5%90%88%e5%b9%b6-2-%e4%b8%aa%e7%bb%93%e6%9e%84%e4%bd%93","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206977\/","title":{"rendered":"\u5728\u8fd0\u884c\u65f6\u6bd4\u8f83\u548c\u5408\u5e76 2 \u4e2a\u7ed3\u6784\u4f53"},"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\u8fd0\u884c\u65f6\u6bd4\u8f83\u548c\u5408\u5e76 2 \u4e2a\u7ed3\u6784\u4f53<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 10:12: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>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60Golang\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u5728\u8fd0\u884c\u65f6\u6bd4\u8f83\u548c\u5408\u5e76 2 \u4e2a\u7ed3\u6784\u4f53\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u5728\u8fd0\u884c\u65f6\u6bd4\u8f83\u4e24\u4e2a\u7ed3\u6784\u3002\u6211\u4f3c\u4e4e\u65e0\u6cd5\u4e00\u4e00\u6bd4\u8f83\u8fd9\u4e2a\u9886\u57df\u3002 \u6211\u60f3\u6211\u9700\u8981\u5728\u8fd0\u884c\u5faa\u73af\u65f6\u4e3a\u6bcf\u4e2a\u5b57\u6bb5\u8f6c\u6362\u7c7b\u578b\uff0c\u4f46\u662f <code>reflect.typeof()<\/code> \u4e0d\u4f1a\u7ed9\u6211\u201c\u7c7b\u578b\u201d\u7684\u9884\u671f\u7ed3\u679c\uff08\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e3a int \/ string \uff09\u3002 \u6211\u60f3\u8fd9\u662f\u56e0\u4e3a\u6211\u63d0\u4f9b\u4e86\u4e00\u4e2a\u63a5\u53e3{}\u4f5c\u4e3a\u53c2\u6570\uff1f\u6709\u4ec0\u4e48\u529e\u6cd5\u8ba9\u5b83\u53d1\u6325\u4f5c\u7528\u5417\uff1f<\/p>\n<p>\u6211\u7684\u76ee\u6807\u662f\u80fd\u591f\u6bd4\u8f83\u76f8\u540c\u7c7b\u578b\u7684 2 \u4e2a\u7ed3\u6784\u4f53\u7684\u503c\uff0c\u5e76\u5728\u5b58\u5728\u4efb\u4f55\u5dee\u5f02\u65f6\u5c06\u8fd9\u4e9b\u503c\u201c\u5408\u5e76\u201d\u5230\u4e00\u4e2a\u7ed3\u6784\u4f53\u4e2d\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"reflect\"\n)\n\ntype A struct {\n    Foo string\n    Bar int\n    Zoo int\n}\n\nfunc main() {\n    a := &amp;A{Foo: \"qwer\",Bar:1}\n    b := &amp;A{Foo: \"zxcv\",Bar:1}\n    testRefactor(a,b)\n\n}\n\nfunc testRefactor(t *A,comp *A) {\n    valt := reflect.ValueOf(t).Elem()\n    \/\/valComp := reflect.ValueOf(comp).Elem()\n    for i:=0; i&lt;valt.NumField();i++{\n        \/\/fieldStructComp := valComp.Type().Field(i).Name\n        fieldStructT := valt.Type().Field(i).Name\n\n\n    valueStructComp := getFieldValueByname(comp,fieldStructT)\n    valueStructT := getFieldValueByname(t,fieldStructT)\n\n    typex := reflect.TypeOf(valueStructT)\n    fmt.Println(typex.String())\n\n        fmt.Println(valueStructT)\n        fmt.Println(valueStructComp)\n        fmt.Println(valueStructT == valueStructComp)\n\n    }\n}\n\nfunc getFieldValueByname(structName interface{},fieldname string) interface{} {\n    r := reflect.ValueOf(structName)\n        f := reflect.Indirect(r).FieldByName(fieldname)\n       return f\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u50cf\u95ee\u9898\u4e2d\u7684\u7b80\u5355\u7ed3\u6784\u53ef\u4ee5\u4e0e <code>==<\/code> \u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<pre>func main() {\n    a := &amp;a{foo: \"qwer\", bar: 1}\n    b := &amp;a{foo: \"zxcv\", bar: 1}\n    c := &amp;a{foo: \"qwer\", bar: 1}\n    d := &amp;a{foo: \"zxcv\", bar: 1}\n\n    fmt.println(*a == *b)\n    fmt.println(*a == *c)\n    fmt.println(*b == *d)\n}<\/pre>\n<\/p>\n<p>\u53e6\u8bf7\u6ce8\u610f\uff0c\u5b57\u6bb5\u7684\u987a\u5e8f\u662f\u9759\u6001\u7684\uff0c\u4e3a\u4e86\u6bd4\u8f83\u76f8\u540c\u7c7b\u578b\u7684\u4e24\u4e2a\u7ed3\u6784\u7684\u503c\uff0c\u60a8\u53ef\u4ee5\u6267\u884c\u57fa\u672c\u5faa\u73af\u5e76\u4f7f\u7528 <code>i<\/code> \u8bbf\u95ee\u4e24\u4e2a\u7ed3\u6784\u5b9e\u4f8b\u7684\u76f8\u5e94\u5b57\u6bb5\uff08\u7c7b\u578b\u548c\u503c\uff09\u3002<\/p>\n<p>\u5373<code>getfieldvaluebyname<\/code> \u51fd\u6570\u662f\u4e0d\u5fc5\u8981\u7684\uff0c\u60a8\u4e0d\u9700\u8981\u5b83\u3002<\/p>\n<pre>func testrefactor(a, b *a) {\n    av := reflect.valueof(a).elem()\n    bv := reflect.valueof(b).elem()\n    at := av.type()\n    bt := bv.type()\n\n    for i := 0; i &lt; av.numfield(); i++ {\n        afv := av.field(i)\n        bfv := bv.field(i)\n\n        aft := at.field(i)\n        bft := bt.field(i)\n\n        fmt.printf(\"a.%s &lt;%s&gt; = %v\\n\", aft.name, aft.type, afv)\n        fmt.printf(\"b.%s &lt;%s&gt; = %v\\n\", bft.name, bft.type, bfv)\n        fmt.printf(\"== ? %t\\n\", afv.interface() == bfv.interface())\n        fmt.println()\n\n    }\n}<\/pre>\n<\/p>\n<p>\u8981\u5408\u5e76\u4e24\u4e2a\u4e0d\u540c\u7ed3\u6784\u7684\u503c\uff0c\u60a8\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u5f00\u59cb\uff1a<\/p>\n<pre>func testRefactor(a, b interface{}) {\n    av := reflect.ValueOf(a).Elem()\n    bv := reflect.ValueOf(b).Elem()\n    at := av.Type()\n    bt := bv.Type()\n\n    for i := 0; i &lt; av.NumField(); i++ {\n        afv := av.Field(i)\n        aft := at.Field(i)\n\n        bfv := bv.FieldByName(aft.Name)\n        bft, ok := bt.FieldByName(aft.Name)\n        if !ok || aft.Type != bft.Type {\n            continue\n        }\n\n        fmt.Printf(\"a.%s &lt;%s&gt; = %v\\n\", aft.Name, aft.Type, afv)\n        fmt.Printf(\"b.%s &lt;%s&gt; = %v\\n\", bft.Name, bft.Type, bfv)\n        fmt.Printf(\"== ? %t\\n\", afv.Interface() == bfv.Interface())\n        fmt.Println()\n\n        if afv.Interface() != bfv.Interface() {\n            afv.Set(bfv)\n        }\n\n    }\n}<\/pre>\n<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5728\u8fd0\u884c\u65f6\u6bd4\u8f83\u548c\u5408\u5e76 2 \u4e2a\u7ed3\u6784\u4f53\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\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-206977","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206977","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=206977"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206977\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}