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 }
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"}
Now, we create a new user2 variable and assign it the value of user1.
var user2 User user2 = user1
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
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) }
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!