{"id":14684,"date":"2024-03-15T09:44:16","date_gmt":"2024-03-15T09:44:16","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/"},"modified":"2025-08-06T12:02:52","modified_gmt":"2025-08-06T12:02:52","slug":"what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/","title":{"rendered":"Deep vs Shallow Copy in Golang"},"content":{"rendered":"<p>In the Go language, deep copy and shallow copy are two concepts related to data duplication.<\/p>\n<p>Shallow copy refers to copying only the reference (address) of a data structure, rather than its actual content. This means that the new and original data structures share the same underlying data, so modifying one will affect the other. In Go language, most built-in types (such as int, string, slice, map, channel, etc.) are shallow copied during assignment operations.<\/p>\n<p>Sample code:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">package<\/span> main\r\n\r\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\r\n\r\n<span class=\"hljs-keyword\">type<\/span> Person <span class=\"hljs-keyword\">struct<\/span> {\r\n    Name <span class=\"hljs-type\">string<\/span>\r\n    Age  <span class=\"hljs-type\">int<\/span>\r\n}\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\r\n    p1 := Person{Name: <span class=\"hljs-string\">\"Alice\"<\/span>, Age: <span class=\"hljs-number\">30<\/span>}\r\n    p2 := p1 <span class=\"hljs-comment\">\/\/ \u6d45\u62f7\u8d1d<\/span>\r\n\r\n    p1.Name = <span class=\"hljs-string\">\"Bob\"<\/span>\r\n    p1.Age = <span class=\"hljs-number\">40<\/span>\r\n\r\n    fmt.Println(p1) <span class=\"hljs-comment\">\/\/ \u8f93\u51fa: {Bob 40}<\/span>\r\n    fmt.Println(p2) <span class=\"hljs-comment\">\/\/ \u8f93\u51fa: {Bob 40}<\/span>\r\n}\r\n<\/code><\/pre>\n<p>In the above example, p1 and p2 share the same Person object, so any changes made to p1 will also affect p2.<\/p>\n<p>Deep copy refers to making a complete copy of a data structure layer by layer, including all content, both base data and referenced data. This means that the two data structures, old and new, are completely independent, so modifying one will not affect the other. In Go language, for custom struct types, deep copy can be achieved through methods like using json.Marshal and json.Unmarshal functions for serialization and deserialization, or manually copying each field layer by layer.<\/p>\n<p>Example code:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">package<\/span> main\r\n\r\n<span class=\"hljs-keyword\">import<\/span> (\r\n    <span class=\"hljs-string\">\"encoding\/json\"<\/span>\r\n    <span class=\"hljs-string\">\"fmt\"<\/span>\r\n)\r\n\r\n<span class=\"hljs-keyword\">type<\/span> Person <span class=\"hljs-keyword\">struct<\/span> {\r\n    Name <span class=\"hljs-type\">string<\/span>\r\n    Age  <span class=\"hljs-type\">int<\/span>\r\n}\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\r\n    p1 := Person{Name: <span class=\"hljs-string\">\"Alice\"<\/span>, Age: <span class=\"hljs-number\">30<\/span>}\r\n    p2 := deepCopy(p1) <span class=\"hljs-comment\">\/\/ \u6df1\u62f7\u8d1d<\/span>\r\n\r\n    p1.Name = <span class=\"hljs-string\">\"Bob\"<\/span>\r\n    p1.Age = <span class=\"hljs-number\">40<\/span>\r\n\r\n    fmt.Println(p1) <span class=\"hljs-comment\">\/\/ \u8f93\u51fa: {Bob 40}<\/span>\r\n    fmt.Println(p2) <span class=\"hljs-comment\">\/\/ \u8f93\u51fa: {Alice 30}<\/span>\r\n}\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">deepCopy<\/span><span class=\"hljs-params\">(src Person)<\/span><\/span> Person {\r\n    <span class=\"hljs-keyword\">var<\/span> dst Person\r\n    data, _ := json.Marshal(src)\r\n    json.Unmarshal(data, &amp;dst)\r\n    <span class=\"hljs-keyword\">return<\/span> dst\r\n}\r\n<\/code><\/pre>\n<p>In the above examples, by using the json.Marshal and json.Unmarshal functions for serialization and deserialization, a deep copy of the Person object is achieved, ensuring that p1 and p2 are completely independent.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the Go language, deep copy and shallow copy are two concepts related to data duplication. Shallow copy refers to copying only the reference (address) of a data structure, rather than its actual content. This means that the new and original data structures share the same underlying data, so modifying one will affect the other. [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[224,3153,4095,9772,3154],"class_list":["post-14684","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-data-structures","tag-deep-copy","tag-go-programming","tag-golang","tag-shallow-copy"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Deep vs Shallow Copy in Golang - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn the key differences between deep copy and shallow copy in Go. Understand how data duplication works in Golang with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Deep vs Shallow Copy in Golang\" \/>\n<meta property=\"og:description\" content=\"Learn the key differences between deep copy and shallow copy in Go. Understand how data duplication works in Golang with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T09:44:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-06T12:02:52+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin Taylor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin Taylor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/\"},\"author\":{\"name\":\"Benjamin Taylor\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\"},\"headline\":\"Deep vs Shallow Copy in Golang\",\"datePublished\":\"2024-03-15T09:44:16+00:00\",\"dateModified\":\"2025-08-06T12:02:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/\"},\"wordCount\":220,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"data structures\",\"deep copy\",\"Go programming\",\"Golang\",\"shallow copy\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/\",\"name\":\"Deep vs Shallow Copy in Golang - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T09:44:16+00:00\",\"dateModified\":\"2025-08-06T12:02:52+00:00\",\"description\":\"Learn the key differences between deep copy and shallow copy in Go. Understand how data duplication works in Golang with examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deep vs Shallow Copy in Golang\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\",\"name\":\"Benjamin Taylor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"caption\":\"Benjamin Taylor\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Deep vs Shallow Copy in Golang - Blog - Silicon Cloud","description":"Learn the key differences between deep copy and shallow copy in Go. Understand how data duplication works in Golang with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/","og_locale":"en_US","og_type":"article","og_title":"Deep vs Shallow Copy in Golang","og_description":"Learn the key differences between deep copy and shallow copy in Go. Understand how data duplication works in Golang with examples.","og_url":"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T09:44:16+00:00","article_modified_time":"2025-08-06T12:02:52+00:00","author":"Benjamin Taylor","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Benjamin Taylor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/"},"author":{"name":"Benjamin Taylor","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9"},"headline":"Deep vs Shallow Copy in Golang","datePublished":"2024-03-15T09:44:16+00:00","dateModified":"2025-08-06T12:02:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/"},"wordCount":220,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["data structures","deep copy","Go programming","Golang","shallow copy"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/","url":"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/","name":"Deep vs Shallow Copy in Golang - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T09:44:16+00:00","dateModified":"2025-08-06T12:02:52+00:00","description":"Learn the key differences between deep copy and shallow copy in Go. Understand how data duplication works in Golang with examples.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-difference-between-deep-copy-and-shallow-copy-in-golang\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Deep vs Shallow Copy in Golang"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9","name":"Benjamin Taylor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","caption":"Benjamin Taylor"},"url":"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=14684"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14684\/revisions"}],"predecessor-version":[{"id":158722,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14684\/revisions\/158722"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=14684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=14684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=14684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}