goext/langext/object.go

20 lines
274 B
Go
Raw Normal View History

2024-04-07 15:10:21 +02:00
package langext
import "encoding/json"
func DeepCopyByJson[T any](v T) (T, error) {
bin, err := json.Marshal(v)
if err != nil {
return *new(T), err
}
var result T
err = json.Unmarshal(bin, &result)
if err != nil {
return *new(T), err
}
return result, nil
}