{"id":10006863,"date":"2022-11-17T02:56:04","date_gmt":"2022-11-16T18:56:04","guid":{"rendered":"https:\/\/ityet.com\/?p=10006863"},"modified":"2022-11-17T02:56:04","modified_gmt":"2022-11-16T18:56:04","slug":"golang-copier%e5%85%a5%e9%97%a8%e5%88%b0%e5%85%a5%e5%9d%91","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006863","title":{"rendered":"Golang Copier\u5165\u95e8\u5230\u5165\u5751"},"content":{"rendered":"<p>github: <a href=\"https:\/\/link.segmentfault.com\/?enc=%2F86LJfsH%2B3xQ%2Fq9dNuKIHA%3D%3D.JaNai1H%2Fjqeiw2gHFiXMESR1eW7JfXI5IEG2LSPIMGtmImeFeaPB9i88OSSKt1vn\" rel=\"nofollow\">https:\/\/github.com\/jinzhu\/copier<\/a><\/p>\n<p>\u7531\u4e8e <code>golang<\/code> \u6ca1\u6709\u5bf9\u590d\u6742\u7ed3\u6784\u4f53\u7684 <code>clone<\/code> \u65b9\u6cd5\uff0c\u6240\u4ee5\uff0c\u5c31\u9700\u8981\u6709 <code>copier<\/code> \u8fd9\u6837\u7684\u5de5\u5177\u5e93\u3002<\/p>\n<p>\u5b83\u770b\u8d77\u6765\u5f88\u7b80\u5355\uff0c\u4f46\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u6709\u4e9b\u201c\u5751\u201d\u8fd8\u662f\u8981\u6ce8\u610f\uff01<\/p>\n<p>\u672c\u6587\uff1a<\/p>\n<p><strong>\u5165\u95e8\u4e3a\u8f85\uff0c\u63a2\u201c\u5751\u201d\u4e3a\u4e3b\uff0c<\/strong><\/p>\n<p><strong>\u770b\u5b8c\u518d\u5212\u8d70\uff0cCS\u6211\u6ca1\u6709\u3002<\/strong><\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc3J1t\" alt=\"\u6ee1\u5634\u987a\u53e3\u6e9c\" title=\"\u6ee1\u5634\u987a\u53e3\u6e9c\" loading=\"lazy\"><\/p>\n<h2>\u5b89\u88c5<\/h2>\n<pre><code class=\"shell\">go get github.com\/jinzhu\/copier<\/code><\/pre>\n<h2>\u5feb\u901f\u5165\u95e8<\/h2>\n<p>\u597d\u7684\uff0c\u6765\u4e00\u6bb5\u4ee3\u7801\u5feb\u901f\u4e86\u89e3 <code>copier<\/code><\/p>\n<pre><code class=\"golang\">package main\n\nimport (\n    &quot;fmt&quot;\n    &quot;github.com\/jinzhu\/copier&quot;\n)\n\ntype SC struct {\n    C uint8\n}\n\n\ntype M1 struct {\n    A int\n    W string\n    S *SC\n}\n\nfunc main() {\n    var src = M1{12, &quot;Hello&quot;, &amp;SC{32}}\n    var dst = M1{}\n    fmt.Printf(&quot;before copy src %+v\/tdst %+v\/n&quot;, src, dst)\n    copier.Copy(&amp;dst, src)\n    fmt.Printf(&quot;after  copy src %+v\/tdst %+v\/n&quot;, src, dst)\n}<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre><code>before copy src {A:12 W:Hello S:0xc00017f550}   dst {A:0 W: S:&lt;nil&gt;}\nafter  copy src {A:12 W:Hello S:0xc00017f550}   dst {A:12 W:Hello S:0xc00017f618}<\/code><\/pre>\n<p>\u597d\u7684\uff0c\u770b\u5230\u8fd9\uff0c\u4f60\u5c31\u5df2\u638c\u63e1\u4e86 <code>copier<\/code> 80%\u7684\u529f\u80fd\u4e86\u3002\u5148\u522b\u7740\u6025\u5212\u8d70\uff0c\u63a5\u4e0b\u6765\u8fd8\u662f\u8e29\u5751\u8bb0\u5f55\u3002<\/p>\n<blockquote>\n<p>\u672c\u6587\u4ee3\u7801\u8fd0\u884c\u8f93\u51fa\u5185\u5bb9\u662f\u57fa\u4e8e github.com\/jinzhu\/copier@v0.3.5 \u548c go1.16.1 darwin\/amd64 \u73af\u5883\u6f14\u793a\u7684\u7ed3\u679c\u3002<\/p>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=oTVMD6Bfy4Q9krUoGYeVqA%3D%3D.Csi1fnVMvo%2FzHUV0OZxfIR0rtcIzpKkEeVwrpGVivNg%3D\" rel=\"nofollow\">http:\/\/itjsz.com<\/a><\/p>\n<\/blockquote>\n<h2>\u5165\u5751<\/h2>\n<pre><code class=\"golang\">package main\n\nimport (\n    &quot;fmt&quot;\n    &quot;github.com\/davecgh\/go-spew\/spew&quot;\n    &quot;github.com\/jinzhu\/copier&quot;\n)\n\ntype SC struct {\n    C uint8\n}\n\ntype Map1 struct {\n    M map[string]int32\n    A []int32\n    C *SC\n}\n\nfunc main() {\n    var src = Map1{map[string]int32{&quot;C:&quot;: 3, &quot;d&quot;: 4}, []int32{9, 8}, &amp;SC{32}}\n    var dst1 = Map1{}\n    spew.Printf(&quot;before src %+v\/t\/tdst %+v\/n&quot;, src, dst1)\n    copier.Copy(&amp;dst1, src)\n    dst1.M[&quot;F&quot;] = 5\n    dst1.M[&quot;g&quot;] = 6\n    dst1.A[0] = 7\n    dst1.C.C = 27\n    spew.Printf(&quot;after  src %+v\/tdst %+v\/n&quot;, src, dst1)\n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u8fd0\u884c\u540e\u4f1a\u8f93\u51fa\uff1a<\/p>\n<pre><code>before src {M:map[C::3 d:4] A:[9 8] C:&lt;*&gt;(0xc00012a1e8){C:32}}          dst {M:&lt;nil&gt; A:&lt;nil&gt; C:&lt;nil&gt;}<\/code><\/pre>\n<p>befre \u90a3\u4e00\u884c\u4ee3\u7801\u5982\u4e0a\u2b06\ufe0f \uff0c after \u90a3\u4e00\u884c\u4f1a\u8f93\u51fa\u4ec0\u4e48\u5462\uff1f<\/p>\n<pre><code>1. after  src {M:map[C::3 d:4] A:[9 8] C:&lt;*&gt;(0xc00012a1e8){C:27}}  dst {M:map[C::3 d:4 F:5 g:6] A:[7 8] C:&lt;*&gt;(0xc00012a348){C:27}}\n2. after  src {M:map[C::3 d:4] A:[9 8] C:&lt;*&gt;(0xc00012a1e8){C:32}}  dst {M:map[C::3 d:4 F:5 g:6] A:[7 8] C:&lt;*&gt;(0xc00012a348){C:27}}\n3. after  src {M:map[C::3 d:4] A:[7 8] C:&lt;*&gt;(0xc00012a1e8){C:32}}  dst {M:map[C::3 d:4 F:5 g:6] A:[7 8] C:&lt;*&gt;(0xc00012a348){C:27}}\n4. after  src {M:map[C::3 d:4 F:5 g:6] A:[7 8] C:&lt;*&gt;(0xc00012a1e8){C:32}}  dst {M:map[C::3 d:4 F:5 g:6] A:[7 8] C:&lt;*&gt;(0xc00012a348){C:27}}\n5. after  src {M:map[C::3 d:4 F:5 g:6] A:[7 8] C:&lt;*&gt;(0xc00012a1e8){C:27}}  dst {M:map[C::3 d:4 F:5 g:6] A:[7 8] C:&lt;*&gt;(0xc00012a348){C:27}}<\/code><\/pre>\n<p>\u7b54\u6848\u662f\uff1a  var a = int(759 \/ 6 \/ 31.5)<\/p>\n<blockquote><p>\u4e3a\u4e86\u907f\u514d\u4e0d\u5c0f\u5fc3\u770b\u4e86\u7b54\u6848\uff0c\u8bf7\u8ba1\u7b97 759 \/ 6 \/ 31.5 \u5f97\u51fa\u7684\u503c\u56db\u820d\u4e94\u5165\u4fbf\u662f\u3002<\/p><\/blockquote>\n<h2>\u518d\u63a2\u5751<\/h2>\n<h2>\u51fa\u5751<\/h2>\n<p>\u6211\u770b\u5176\u4ed6\u540c\u5b66\u4f7f\u7528 <code>copier<\/code> \u4e5f\u662f\u50cf\u4e0a\u9762\u90a3\u6837\u2014\u2014<code>copier.Copy($dst, src)<\/code>, \u5f53\u7136\u5566\uff0c\u4e5f\u4e0d\u6392\u9664\u6211\uff01\u5b83\u4eff\u4f5b\u5c31\u662f\u4e00\u628a\u5c0f\u5de7\u7cbe\u608d\u7684\u5c0f\u5200\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u8c03\u7528\uff0c\u5c31\u5b8c\u6210\u4e86\u5b83\u7684\u4f7f\u547d\u3002<\/p>\n<p>\u7136\u800c\uff0c\u5b83\u5176\u5b9e\u662f\u628a\u591a\u529f\u80fd\u5200\uff0c\u6211\u90fd\u8fd8\u6ca1\u6709\u6253\u5f00\u5b83\u7684\u2014\u2014 <code>option<\/code>\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u95ee\u9898\u5c31\u662f\uff0c\u6211 Copy \u540e\uff0c\u5bf9\u503c\u7684\u6539\u52a8\uff0c\u5f71\u54cd\u4e86\u53e6\u4e00\u4e2a\u503c\u7684 map\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65f6\u5019\uff0c\u5c31\u9700\u8981\u8fdb\u884c\u6df1 copy\u3002\u63a5\u4e0b\u6765\u5f15\u5165 <code>copier<\/code> \u7684 <code>option<\/code><\/p>\n<pre><code>package main\n\nimport (\n    &quot;fmt&quot;\n    &quot;github.com\/davecgh\/go-spew\/spew&quot;\n    &quot;github.com\/jinzhu\/copier&quot;\n)\n\ntype SC struct {\n    C uint8\n}\n\ntype Map1 struct {\n    M map[string]int32\n    A []int32\n    C *SC\n}\n\nfunc main() {\n    var src = Map1{map[string]int32{&quot;C:&quot;: 3, &quot;d&quot;: 4}, []int32{9, 8}, &amp;SC{32}}\n    var dst1 = Map1{}\n    spew.Printf(&quot;before src %+v\/t\/tdst %+v\/n&quot;, src, dst1)\n    \n    copier.CopyWithOption(&amp;dst1, src, copier.Option{DeepCopy: true})   \/\/ \u8fd9\u91cc\uff01\n    \n    dst1.M[&quot;F&quot;] = 5\n    dst1.M[&quot;g&quot;] = 6\n    dst1.A[0] = 7\n    dst1.C.C = 27\n    spew.Printf(&quot;after  src %+v\/tdst %+v\/n&quot;, src, dst1)\n}<\/code><\/pre>\n<p>\u597d\u7684\uff0c\u8fd9\u6837copy\u4e4b\u540e\uff0c\u5bf9\u65b0\u53d8\u91cf\u7684\u6539\u52a8\uff0c\u4e0d\u4f1a\u4f20\u9012\u4f1a\u539f\u53d8\u91cf\u6539\u52a8\u4e86\u3002<\/p>\n<h2>\u518d\u76d8\u4e00\u76d8\u5751<\/h2>\n<pre><code class=\"golang\">package main\n\nimport (\n    &quot;fmt&quot;\n\n    &quot;github.com\/davecgh\/go-spew\/spew&quot;\n    &quot;github.com\/jinzhu\/copier&quot;\n)\n\ntype ArrTC struct {\n    Name [2]string\n    C    *ArrTC\n}\n\ntype ArrT struct {\n    A  [3]int32\n    S  []int32\n    E  []int32\n    C  string\n    V  string\n    M map[string]int32\n    AC ArrTC\n    s bool\n}\n\nfunc main() {\n    var src = ArrT{\n        [3]int32{9, 10, 0},\n        []int32{12, 0},\n        []int32{},\n        &quot;&quot;,\n        &quot;val&quot;,\n        map[string]int32{&quot;A:&quot;: 1, &quot;b&quot;: 0},\n        ArrTC{},\n        true,\n    }\n    var dst = ArrT{\n        [3]int32{1, 2, 3},\n        []int32{4, 5, 6, 7},\n        []int32{9, 10},\n        &quot;char&quot;,\n        &quot;ha&quot;,\n        map[string]int32{&quot;C:&quot;: 3, &quot;b&quot;: 4, &quot;.&quot;: 0},\n        ArrTC{[2]string{&quot;Y&quot;, &quot;Z&quot;}, nil},\n        false,\n    }\n    spew.Printf(&quot;before src %+v\/tdst %+v\/n&quot;, src, dst)\n    copier.CopyWithOption(&amp;dst, src, copier.Option{IgnoreEmpty: true, DeepCopy: true})\n    spew.Printf(&quot;after  src %+v\/tdst %+v\/n&quot;, src, dst)\n    src.M[&quot;b&quot;] = 99\n    src.S[1] = 1\n    dst.S[0] = 2\n    spew.Printf(&quot;last  src %+v\/tdst %+v\/n\/n&quot;, src, dst)\n}<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre><code>before src {A:[9 10 0] S:[12 0] E:[] C: V:val M:map[A::1 b:0] AC:{Name:[ ] C:&lt;nil&gt;} s:true}    dst {A:[1 2 3] S:[4 5 6 7] E:[9 10] C:char V:ha M:map[C::3 b:4 .:0] AC:{Name:[Y Z] C:&lt;nil&gt;} s:false}\nafter  src {A:[9 10 0] S:[12 0] E:[] C: V:val M:map[A::1 b:0] AC:{Name:[ ] C:&lt;nil&gt;} s:true}    dst {A:[9 10 0] S:[12 0 6 7] E:[9 10] C:char V:val M:map[A::1 C::3 b:0 .:0] AC:{Name:[Y Z] C:&lt;nil&gt;} s:true}\nlast  src {A:[9 10 0] S:[12 1] E:[] C: V:val M:map[A::1 b:99] AC:{Name:[ ] C:&lt;nil&gt;} s:true}    dst {A:[9 10 0] S:[2 0 6 7] E:[9 10] C:char V:val M:map[C::3 b:0 .:0 A::1] AC:{Name:[Y Z] C:&lt;nil&gt;} s:true}<\/code><\/pre>\n<p>\u8fd9\u6b21\u7684\u4ee3\u7801\u6211\u52a0\u4e0a\u4e86 <code>IgnoreEmpty: true<\/code>, \u4e5f\u5c31\u662f\u590d\u5236\u65f6\u5ffd\u7565\u7a7a\u7684\u503c\u3002 \u4e5f\u5c31\u8bf4\u53ef\u4ee5\u5f53\u4f5c\u503c merge \u7528\u3002<\/p>\n<p>\u7136\u540e\uff0c\u53c8\u6d4b\u8bd5\u4e86\u4e00\u4e0b\u53d8\u91cf\u72ec\u7acb\u6027\u3002\u590d\u5236\u4e4b\u540e\uff0c<code>src<\/code>, <code>dst<\/code>  \u4e24\u4e2a\u53d8\u91cf\u518d\u65e0\u74dc\u845b\uff0c\u5bf9\u5176\u4e2d\u4e00\u4e2a\u503c\u7684\u4efb\u610f\u6539\u52a8\u90fd\u4e0d\u4f1a\u540c\u6b65\u5230\u53e6\u4e00\u4e2a\u503c\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u8fd9\u4e2a <code>merge<\/code> \u7684\u8868\u73b0\uff0c\u53ef\u80fd\u4e0d\u662f\u4f60\u60f3\u7684\u90a3\u6837\uff0c<\/p>\n<pre><code>src.S = []int32{12, 0}\ndst.S = []int32{4, 5, 6, 7}\n## \u8c03\u7528 copy \u540e\uff0c \u4f60\u9884\u671f\u7684\u7ed3\u679c\u662f\u4ec0\u4e48\uff1f[6\/7]\n6. dst.S = []int32{12, 0}\n7. dst.S = []int32{12, 0, 6, 7}<\/code><\/pre>\n<ul>\n<li>\u9009\u98796: \u55ef\uff0c\u539f\u6765\u662f {12, 0} \u590d\u5236\u7ed9 dst \u5c31\u662f {12, 0}<\/li>\n<li>\u9009\u98797: \u8fd9\u4e2a\u662f\u5207\u7247\uff0c\u4f60\u53ea\u7ed9\u6211 0\uff0c1 \u4f4d\u7684\u503c\uff0ccopier\u628a 0\uff0c1 \u4f4d\u7f6e\u7684\u503c <code>copy<\/code> \u4e86\uff0c<code>dst<\/code>\u540e\u97622,3\u4f4d\u7684\u503c\uff0c<code>src<\/code>\u6ca1\u7ed9\u51fa\uff0c\u90a3\u5c31\u4e0d\u7ba1\u3002\u6240\u4ee5\u5c31\u662f {12, 0, 6, 7}<\/li>\n<\/ul>\n<p>\u8fd9\u5757\u7684\u8868\u73b0\uff0c\u6211\u89c9\u5f97\u662f\u6709\u4e89\u8bae\u7684\uff0c\u5927\u4f6c\u4eec\u5728\u8bc4\u8bba\u533a\u7559\u4e0b\u4f60\u9884\u671f\u9009\u9879\uff0c\u770b\u770b\u5927\u5bb6\u662f\u4e0d\u662f\u90fd\u8fd9\u6837\u60f3\u7684\u3002<\/p>\n<p>\u5b9e\u9645\u8fd0\u884c\u7ed3\u679c\uff0c\u89c1\u4e0a\u9762\u7684\u4ee3\u7801\u8f93\u51fa\u5c31\u80fd\u627e\u5230\u7b54\u6848\u3002<\/p>\n<h2>\u7ed3\u8bed<\/h2>\n<p><code>copier<\/code> \u672c\u6765\u662f\u4e00\u4e2a\u77ed\u5c0f\u7cbe\u608d\u7684\u5de5\u5177\u5e93\uff0c\u4e5f\u6ca1\u60f3\u8981\u6c34\u4e00\u7bc7\uff0c\u6700\u8fd1\u4f7f\u7528\u65f6\uff0c\u7a81\u7136\u8e29\u5751\uff0c\u5c31\u7279\u5f00\u4e00\u7bc7\uff0c\u548c\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\u8e29\u5751\u7ecf\u9a8c\u3002<\/p>\n<p>\u5728\u4f7f\u7528\u5916\u90e8\u5e93\u7684\u65f6\u5019\uff0c\u8fd8\u662f\u5efa\u8bae\u53bb <a href=\"https:\/\/link.segmentfault.com\/?enc=n1AMPWAjZEOIIPw%2F278fTw%3D%3D.rj76gVHy%2FVDjCE4oX13WEyXOwIZgoPIEbsf6zoxdiizLDP6H4zFR%2F0LwCcFMw%2B8P\" rel=\"nofollow\">github<\/a> \u4e0a\u770b\u770b\u8be6\u7ec6\u8bf4\u660e, \u6216\u8005\u4e0a <a href=\"https:\/\/link.segmentfault.com\/?enc=RAK27ndNQ5K614w510KWEQ%3D%3D.48%2FLgixJ9WhYkaJTgEaulRAScAN3tXGZpw5mzPsnORQ%3D\" rel=\"nofollow\">pkg.go.dev<\/a> \u770b\u770b\u5b83\u66b4\u9732\u51fa\u6765\u51fa\u7684\u63a5\u53e3\u4ee5\u53ca\u8bf4\u660e\u3002\u66f4\u6216\u8005\u8fdb\u884c\u5b8c\u6574\u7684\u6d4b\u8bd5\uff0c\u5145\u5206\u4e86\u89e3\u5b83\u4e4b\u540e\uff0c\u518d\u4f7f\u7528\u3002<\/p>\n<blockquote><p>PS\uff1a \u6700\u8fd1\u9879\u76ee\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u5e93\uff0c\u53d1\u73b0\u95ee\u9898\u540e\uff0c\u6539\u75af\u4e86\uff5e<\/p><\/blockquote>\n<p>\u672c\u6587\u53c2\u4e0e\u4e86<a href=\"https:\/\/segmentfault.com\/a\/1190000042741155\">\u601d\u5426\u6280\u672f\u5f81\u6587<\/a>\uff0c\u6b22\u8fce\u6b63\u5728\u9605\u8bfb\u7684\u4f60\u4e5f\u52a0\u5165\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042798553\">https:\/\/segmentfault.com\/a\/1190000042798553<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>github: https:\/\/github.com\/jinzhu\/copier \u7531\u4e8e golang \u6ca1\u6709\u5bf9\u590d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006863","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006863"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006863\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}