Deep copy and shallow copy of Go language explanation

Keywords: Attribute

We often copy one variable to another in development, so this process may be deep and shallow copy. Today, let's help you distinguish the differences between the two copies.

1, Concept

1. Deep Copy:

Copying is the data itself, creating a new object. The newly created object does not share memory with the original object. The newly created object creates a new memory address in memory, and the value of the original object will not be affected when the new object value is modified. Since the memory address is different, when you release the memory address, you can release it separately.

Data of value type is all deep copy by default, Array, Int, String, Struct, Float, Bool.

2. Shallow Copy:

Copy the data address, only copy the pointer to the object. At this time, the memory address of the new object and the old object is the same, and the old object will change when the new object value is modified. When the memory address is released, the memory address is also released.

All data of reference type are light copy, Slice and Map by default.

2, Essential differences:

Whether to actually get (copy) the object entity instead of the reference.

3, How to understand?

For example, when P2 copies P1 and modifies P1 attribute, observe whether P2 attribute will change

1. The property of P2 has changed, indicating that this is a shallow copy, and the memory in the heap is the same value.

p2=&p1 // Shallow copy, p2 as pointer, p1 and p2 share the same memory address

2. The property of P2 has not changed, indicating that this is a deep copy, and the memory in the heap is a different value.

p2=p1 // Deep copy, generating two memory addresses 

4, Demonstration example:

Deep copy example:

package main

import (
   "fmt"
)

// Define a Robot structure
type Robot struct {
   Name  string
   Color string
   Model string
}

func main() {
   fmt.Println("As with deep copy content, when you change the value of one object, the other does not change.")
   robot1 := Robot{
      Name:  "Xiao Bai-X type-V1.0",
      Color: "white",
      Model: "small-scale",
   }
   robot2 := robot1
   fmt.Printf("Robot 1: %s\t Memory address:%p \n", robot1, &robot1)
   fmt.Printf("Robot 2: %s\t Memory address:%p \n", robot2, &robot2)

   fmt.Println("modify Robot1 Of Name Property value")
   robot1.Name = "Xiao Bai-X type-V1.1"

   fmt.Printf("Robot 1: %s\t Memory address:%p \n", robot1, &robot1)
   fmt.Printf("Robot 2: %s\t Memory address:%p \n", robot2, &robot2)

}

Operation result:

As with deep copy content, when you change the value of one object, the other does not change.
Robot 1: {small white-X-type-V1.0 white small} memory address: 0xc0000072330
 Robot 2: {small white-X-type-V1.0 white small} memory address: 0xc0000072360
 Modify the Name property value of Robot1
 Robot 1: {small white-X-type-V1.1 white small} memory address: 0xc0000072330
 Robot 2: {small white-X-type-V1.0 white small} memory address: 0xc0000072360

In the deep copy, we can see that the address of Robot1 is different from the memory address of Robot2. When the Name attribute of Robot1 is modified, Robot2 will not change.

We use two methods to introduce shallow copy.

Shallow copy example 1:

package main

import (
   "fmt"
)

// Define a Robot structure
type Robot struct {
   Name  string
   Color string
   Model string
}

func main() {

   fmt.Println("The shallow copy content is the same as the memory address. When you change the value of one object, the other changes at the same time.")
   robot1 := Robot{
      Name:  "Xiao Bai-X type-V1.0",
      Color: "white",
      Model: "small-scale",
   }
   robot2 := &robot1
   fmt.Printf("Robot 1: %s\t Memory address:%p \n", robot1, &robot1)
   fmt.Printf("Robot 2: %s\t Memory address:%p \n", robot2, robot2)

   fmt.Println("Modify it here Robot1 Of Name and Color attribute")
   robot1.Name = "Small black-X type-V1.1"
   robot1.Color = "black"

   fmt.Printf("Robot 1: %s\t Memory address:%p \n", robot1, &robot1)
   fmt.Printf("Robot 2: %s\t Memory address:%p \n", robot2, robot2)

}

Run result 1:

The shallow copy content is the same as the memory address. When you change the value of one object, the other changes at the same time.
Robot 1: {small white-X-type-V1.0 white small} memory address: 0xc0000062330
 Robot 2: & {small white-X-type-V1.0 white small} memory address: 0xc0000062330
 Modify the Name and Color properties of Robot1 here
 Robot 1: {small black - type X - V1.1 black small} memory address: 0xc0000062330
 Robot 2: & {small black-X-type-V1.1 black small} memory address: 0xc0000062330

In the shallow copy, we can see that the memory addresses of Robot1 and Robot2 are the same. When you modify the properties of one object, the other will also change.

Shallow copy example 2:

package main

import (
   "fmt"
)

// Define a Robot structure
type Robot struct {
   Name  string
   Color string
   Model string
}

func main() {

   fmt.Println("Shallow copy use new mode")
   robot1 := new(Robot)
   robot1.Name = "Xiao Bai-X type-V1.0"
   robot1.Color = "white"
   robot1.Model = "small-scale"

   robot2 := robot1
   fmt.Printf("Robot 1: %s\t Memory address:%p \n", robot1, robot1)
   fmt.Printf("Robot 2: %s\t Memory address:%p \n", robot2, robot2)

   fmt.Println("Modify it here Robot1 Of Name and Color attribute")
   robot1.Name = "Blue-X type-V1.2"
   robot1.Color = "blue"

   fmt.Printf("Robot 1: %s\t Memory address:%p \n", robot1, robot1)
   fmt.Printf("Robot 2: %s\t Memory address:%p \n", robot2, robot2)
}

Operation result:

Use new mode for shallow copy
 Robot 1: & {small white-X-type-V1.0 white small} memory address: 0xc0000068330
 Robot 2: & {small white-X-type-V1.0 white small} memory address: 0xc0000068330
 Modify the Name and Color properties of Robot1 here
 Robot 1: & {small black-X-type-V1.2 black small} memory address: 0xc0000068330
 Robot 2: & {small black-X-type-V1.2 black small} memory address: 0xc0000068330

new operation, robot2: = robot1, it looks like a deep copy, but it's actually a shallow copy. Two pointers of robot2 and robot1 share the same memory address.

 

160 original articles published· Liked 22· 240000 visitors+
Private letter follow

Posted by shdt on Mon, 20 Apr 2020 02:18:07 -0700