Home > Backend Development > Golang > How to Properly Copy Interface Values in Go to Avoid Shared Underlying Data?

How to Properly Copy Interface Values in Go to Avoid Shared Underlying Data?

Barbara Streisand
Release: 2024-12-06 17:46:15
Original
534 people have browsed it

How to Properly Copy Interface Values in Go to Avoid Shared Underlying Data?

Copying Interface Values in Go

Interface values in Go encapsulate a dynamic type and an underlying concrete value. Assigning one interface value to another copies the underlying value, not the original concrete value itself. This can lead to unexpected behavior when working with interfaces and pointers to structs.

Consider the following example:

type User interface {
    Name() string
    SetName(name string)
}

type Admin struct {
    name string
}

func (a *Admin) Name() string {
    return a.name
}

func (a *Admin) SetName(name string) {
    a.name = name
}
Copy after login

In this example, Admin implements the User interface. We create a user1 variable of type User and initialize it with a pointer to an Admin struct.

var user1 User
user1 = &Admin{name: "user1"}
Copy after login

Now, we create a new user2 variable and assign it the value of user1.

var user2 User
user2 = user1
Copy after login

The problem here is that both user1 and user2 refer to the same Admin instance. Changing the name of user2 also changes the name of user1. This is because the interface value only contains a pointer to the underlying Admin struct.

To prevent this, we need to create a new Admin struct for user2. We can use reflection to accomplish this:

var user2 User
padmin := user1.(*Admin) // Obtain *Admin pointer
admin2 := *padmin        // Make a copy of the Admin struct
user2 = &admin2          // Wrap its address in another User
Copy after login

Now, changing the name of user2 no longer affects user1.

However, this solution requires us to know the concrete type of the interface value in advance. A more general solution is to use reflection to create a new instance of the dynamic type of the interface value:

var user2 User
if reflect.TypeOf(user1).Kind() == reflect.Ptr {
    // Pointer:
    user2 = reflect.New(reflect.ValueOf(user1).Elem().Type()).Interface().(User)
} else {
    // Not pointer:
    user2 = reflect.New(reflect.TypeOf(user1)).Elem().Interface().(User)
}
Copy after login

This solution works with both pointer and non-pointer interface values.

The above is the detailed content of How to Properly Copy Interface Values in Go to Avoid Shared Underlying Data?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template