{"id":207154,"date":"2025-07-08T10:16:15","date_gmt":"2025-07-08T02:16:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207154\/"},"modified":"2025-07-08T10:16:15","modified_gmt":"2025-07-08T02:16:15","slug":"%e5%9c%a8-golang-%e4%b8%ad%e6%b7%b1%e5%ba%a6%e5%a4%8d%e5%88%b6%e5%9c%b0%e5%9b%be","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207154\/","title":{"rendered":"\u5728 Golang \u4e2d\u6df1\u5ea6\u590d\u5236\u5730\u56fe"},"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 Golang \u4e2d\u6df1\u5ea6\u590d\u5236\u5730\u56fe<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 14:06: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>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728 Golang \u4e2d\u6df1\u5ea6\u590d\u5236\u5730\u56fe\u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u636e\u6211\u4e86\u89e3\uff0c\u5730\u56fe\u662f go \u4e2d\u7684\u5f15\u7528\u7c7b\u578b\u3002\u6240\u4ee5\u8d4b\u503c\u4f1a\u505a\u6d45\u62f7\u8d1d\u3002\u6211\u8ba1\u5212\u5728 golang \u4e2d\u5bf9 maps \u8fdb\u884c\u9012\u5f52\u6df1\u5ea6\u590d\u5236\u3002\u9012\u5f52\uff0c\u56e0\u4e3a\u6211\u6b63\u5728\u5904\u7406\u4e00\u4e2a\u5305\u542b json \u672a\u7f16\u7ec4\u5185\u5bb9\u7684\u6620\u5c04\u3002<\/p>\n<pre>func deepCopyJSON(src map[string]interface{}, dest *map[string]interface{}) error {\n    if src == nil || dest == nil {\n        return errors.New(\"src\/dest is nil. You cannot insert to a nil map\")\n    }\n    for key, value := range src {\n        if reflect.TypeOf(value).String() != jsonType {\n            (*dest)[key] = value\n        } else {\n            (*dest)[key] = make(map[string]int)\n\/\/Suspect code below causes the error.\n            deepCopyJSON(value.(map[string]interface{}), &amp;(((*dest)[key]).(map[string]interface{})))\n        }\n    }\n    return nil\n}<\/pre>\n<p>\u9519\u8bef\uff1a\u65e0\u6cd5\u83b7\u53d6 (*dest)[key] \u7684\u5730\u5740\u3002(map[string]interface {}) \u6211\u8be5\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1f\u8fd8\u6709\u5176\u4ed6\u6df1\u5ea6\u5730\u56fe\u7684\u65b9\u6cd5\u5417\uff1f<\/p>\n<p>\u6211\u521d\u6b65\u4e86\u89e3\u4e86 golang \u4e2d\u5730\u56fe\u7684\u5185\u90e8\u7ed3\u6784\uff0c\u4e5f\u4f1a\u5f88\u6709\u7528\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<pre>func deepcopyjson(src map[string]interface{}, dest map[string]interface{}) error {\n    if src == nil {\n        return errors.new(\"src is nil. you cannot read from a nil map\")\n    }\n    if dest == nil {\n        return errors.new(\"dest is nil. you cannot insert to a nil map\")\n    }\n    jsonstr, err := json.marshal(src)\n    if err != nil {\n        return err\n    }\n    err = json.unmarshal(jsonstr, &amp;dest)\n    if err != nil {\n        return err\n    }\n    return nil\n}<\/pre>\n<p>\u6211\u60f3\u5230\u4e862\u4e2a\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>1) \u53ea\u9700\u5c06\u5176\u5408\u5e76\u56de\u6765\uff0c\u7136\u540e\u590d\u5236\u5b57\u8282\u5373\u53ef\u3002<\/p>\n<p>2\uff09\u9012\u5f52\u8c03\u7528\u5982\u4e0b\u3002<\/p>\n<pre>func deepCopyJSON(src map[string]interface{}, dest map[string]interface{}) {\n    for key, value := range src {\n        switch src[key].(type) {\n        case map[string]interface{}:\n            dest[key] = map[string]interface{}{}\n            deepCopyJSON(src[key].(map[string]interface{}), dest[key].(map[string]interface{}))\n        default:\n            dest[key] = value\n        }\n    }\n}<\/pre>\n<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5728 Golang \u4e2d\u6df1\u5ea6\u590d\u5236\u5730\u56fe\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\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-207154","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207154","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=207154"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207154\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}