You can define how specific types are copied by providing a CopierFunc. This is useful for types that require special handling (e.g., types with internal state that shouldn't be reflected).
Global Registration:
Register functions in the copystructure.Copiers map. The key must be the reflect.Type of the value.
Local Registration:
Pass a custom map to the Copiers field of a copystructure.Config instance to avoid affecting global state.
Warning: It is unsafe to write to the Copiers map while a copy operation is in progress. If you must modify the map concurrently, wrap both the modifications and the Copy calls in a mutex.
CopierFunc Signature:
type CopierFunc func(interface{}) (interface{}, error)
type MySpecialType struct {
Value int
}
copier := func(v interface{}) (interface{}, error) {
t := v.(MySpecialType)
return MySpecialType{Value: t.Value}, nil
}
// Register globally
copystructure.Copiers[reflect.TypeOf(MySpecialType{})] = copier