So I'm trying to write a function to deep copy one struct to another. It's not necessary that the two structs be the same. The following is what I have come up with but I'm running into some errors.
package main
import (
"encoding/json"
"fmt"
)
type Old struct {
Name string
}
type New struct {
Name string
}
func DeepCopy(oldStruct Old, newStruct *interface{}) {
newJSON, _ := json.Marshal(oldStruct)
_ = json.Unmarshal(newJSON, &newStruct)
return
}
func main() {
old := Old{
Name: "shreesh",
}
new := New{}
DeepCopy(old, &new)
fmt.Println(old, new)
}
go playground: https://go.dev/play/p/TXncaPnQSiT