jinzhu / copier

Copier for golang, copy value from struct to struct and more
MIT License
5.58k stars 490 forks source link

unable to copy struct with nested slices #73

Closed vishvanath-gojek closed 3 years ago

vishvanath-gojek commented 3 years ago

Reproducible Example

type slice0 struct {
    sl1 []slice1
}

type slice1 struct {
    sl2 []slice2
}

type slice2 struct {
    sl2 []int
}

realdata := slice0{[]slice1{slice1{[]slice2{slice2{[]int{1, 2, 3}}}}}}
var copydata slice0
copier.Copy(&copydata, &realdata)
copydata.sl1[0] = slice1{[]slice2{slice2{[]int{10, 20, 30}}}}

Playground example https://play.golang.org/p/drVdfl5hx7M

Description

Running above example results in error index out of range.

math-nao commented 3 years ago

You are using unexported fields in your example. Copying to unexported field cannot be supported. You should use exported fields. See: https://play.golang.org/p/uIV1Sn8cU9I