jinzhu / copier

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

Trying to copy a struct isn't working #42

Open RamVellanki opened 5 years ago

RamVellanki commented 5 years ago

I was trying to use the copier to copy a struct but isn't working for me. Following is the code:

https://goplay.space/#ARoaaKRtM2K

type Cat struct {
    age  int
    name string
}

func main() {
    firstCat := Cat{7, "Wilson"}
    secondCat := Cat{}
    copier.Copy(&secondCat, &firstCat)

    fmt.Println(firstCat)
    fmt.Println(secondCat)
}

I expect the output to print {7, "Wilson"} two times but it prints {7, "Wilson"} {0 }.

ilius commented 5 years ago

Only exported fields are copied. I think because reflect can not set unreported fields.