golang

Deep Copying in Go (Deep Copy)

Deep copy refers to the technique of replicating an extremely identical but independent copy of a given data structure. This includes all nested elements in a recursive manner.

Deep copy comes in play when there is duplication of complex data structures without any shared references to the underlying objects.

A deep copy operation ensures that any changes made to the copied data structure do not affect the original structure and vice versa.

Let us explore the methods of deep copying in Go.

Deep Copy in Go

Method 1: Manual Deep Copy

One way to perform a deep copy is to manually create a new instance of the data structure and recursively copy each element.

An example is as follows:

package main

import "fmt"

func DeepCopyIntSlice(slice [][]int) [][]int {
    copy := make([][]int, len(slice))

    for i, s := range slice {
        copy[i] = make([]int, len(s))
        copy[i] = append(copy[i], s...)
    }

    return copy
}

func main() {
    src := [][]int{{1, 2, 3}, {4, 5, 6}}
    copy := DeepCopyIntSlice(src)

    fmt.Println(src)
    fmt.Println(copy)

}

This should create a copy of the “src” slice into the new copied slice.

Method 2: Using the Deepcopier Library

Luckily, in Go, we can utilize the external packages to perform the deep copy operations. Once such library is the “deepcopier” library which simplifies the deep copying process for structs.

An example is as follows:

package main

import (
    "fmt"

    "github.com/ulule/deepcopier"
)

type Car struct {
    Model      string
    Year       int
    Attributes Attributes
}

type Attributes struct {
    Color         string
    Economy       string
    CruiseControl bool
}

func main() {
    src := Car{
        Model: "BMW x7",
        Year:  2023,
        Attributes: Attributes{
            Color:         "Black",
            Economy:       "10.6l/100km",
            CruiseControl: true,
        },
    }

    copy := Car{}
    deepcopier.Copy(&src).To(&copy)

    copy.Model = "BMW 120i"
    copy.Attributes.CruiseControl = false

    fmt.Println(src)
    fmt.Println(copy)

}

This example uses the “deepcopier” library to copy the “source” struct and all the nested fields. We then make changes to the “copy” struct without affecting the source.

{BMW x7 2023 {Black 10.6l/100km true}}
{BMW 120i 2023 {Black 10.6l/100km false}}

Conclusion

In this tutorial, we learned how to perform the deep copy operations on various data structures in the Go programming language.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list