Skip to content

cannot deep clone nested maps with interface{} types inside #1

@tshi206

Description

@tshi206

Hi just FYI, seems it doesn't deep clone nested maps with interface{} types inside:

package main

import (
	"fmt"

	"github.com/jinzhu/copier"
	"github.com/qdm12/reprint"
)

func main() {
	var fields map[string]interface{}
	msg := "body123"
	vals := []string{"fake1", "fake2", "fake3"}
	for _, regid := range vals {
		fields = map[string]interface{}{
			"message": map[string]interface{}{
				"data": msg,
				"token": regid,
			},
		}
		fmt.Println(fields)
	}
	fmt.Println("Hello World")
	fmt.Println("1", fields)
	tmp := make(map[string]interface{})
	err := reprint.FromTo(&fields, &tmp)
	if err != nil {
		panic(err)
	}
	fmt.Println("2", fields)
	fmt.Println("3", tmp)
	tmp["message"].(map[string]interface{})["token"] = "foobar123"
	fmt.Println("4", fields)
	fmt.Println("5", tmp)
	copier.CopyWithOption(&tmp, &fields, copier.Option{DeepCopy: true})
	tmp["message"].(map[string]interface{})["token"] = "foobar456"
	fmt.Println("6", fields)
	fmt.Println("7", tmp)
}

image
notice println 4 and 5 where both map changed after the foobar123 value assignment

https://goplay.tools/snippet/_TIUbl-e2DT

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions