copy.go 341 B

12345678910111213141516171819
  1. package common
  2. import (
  3. "fmt"
  4. "github.com/jinzhu/copier"
  5. )
  6. func DeepCopy[T any](src *T) (*T, error) {
  7. if src == nil {
  8. return nil, fmt.Errorf("copy source cannot be nil")
  9. }
  10. var dst T
  11. err := copier.CopyWithOption(&dst, src, copier.Option{DeepCopy: true, IgnoreEmpty: true})
  12. if err != nil {
  13. return nil, err
  14. }
  15. return &dst, nil
  16. }