How to Copy Struct Type Using Value and Pointer Reference in Golang?


In Go, you can copy a struct by value or by reference using pointers. When you copy a struct by value, a new copy of the struct is created in memory, and all the fields of the original struct are copied to the new one. On the other hand, when you copy a struct by reference using a pointer, both the original and the copied struct share the same memory address.

In this article, we will discuss how to copy a struct type in Go using value and pointer reference.

Copy Struct Using Value Reference

To copy a struct using value reference, you simply assign the value of one struct to another.

Example

Let's consider the following example −

package main

import "fmt"

type Person struct {
   name string
   age  int
}

func main() {
   // Create a new person
   p1 := Person{name: "John", age: 30}

   // Copy the person by value
   p2 := p1

   // Change the age of p2
   p2.age = 35

   // Print the values of both persons
   fmt.Println(p1) // Output: {John 30}
   fmt.Println(p2) // Output: {John 35}
}

Output

{John 30}
{John 35}

In the above example, we created a struct type named Person with two fields name and age. We then created a new person p1 with the name "John" and age 30. To copy the person p1 to another person p2, we simply assigned the value of p1 to p2. We then changed the age of p2 to 35 and printed the values of both persons. As you can see, changing the age of p2 did not affect the age of p1.

Copy Struct Using Pointer Reference

To copy a struct using pointer reference, you need to create a new struct and then assign the memory address of the original struct to it.

Example

Let's consider the following example −

package main

import "fmt"

type Person struct {
   name string
   age  int
}

func main() {
   // Create a new person
   p1 := &Person{name: "John", age: 30}

   // Copy the person by reference
   p2 := p1

   // Change the age of p2
   p2.age = 35

   // Print the values of both persons
   fmt.Println(*p1) // Output: {John 35}
   fmt.Println(*p2) // Output: {John 35}
}

Output

{John 35}
{John 35}

In the above example, we created a struct type named Person with two fields name and age. We then created a new person p1 with the name "John" and age 30 using a pointer reference. To copy the person p1 to another person p2, we assigned the memory address of p1 to p2. We then changed the age of p2 to 35 and printed the values of both persons. As you can see, changing the age of p2 affected the age of p1 because both the original and the copied structs share the same memory address.

Conclusion

In Go, you can copy a struct by value or by reference using pointers. When you copy a struct by value, a new copy of the struct is created in memory, and all the fields of the original struct are copied to the new one. On the other hand, when you copy a struct by reference using a pointer, both the original and the copied struct share the same memory address. You should choose the appropriate method based on your use case.

Updated on: 05-May-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements