golang.design/x/reflect@v0.0.0-20220504060917-02c43be63f3b/README.md (about) 1 # reflect 2 3 Package reflect implements the proposal [go.dev/issue/51520](https://go.dev/issue/51520). 4 5 ```go 6 // DeepCopy copies src to dst recursively. 7 // 8 // Two values of identical type are deeply copied if one of the following 9 // cases apply. 10 // 11 // Numbers, bools, strings are deeply copied and have different underlying 12 // memory address. 13 // 14 // Slice and Array values are deeply copied, including its elements. 15 // 16 // Map values are deeply copied for all of its key and corresponding 17 // values. 18 // 19 // Pointer values are deeply copied for their pointed value, and the 20 // pointer points to the deeply copied value. 21 // 22 // Struct values are deeply copied for all fields, including exported 23 // and unexported. 24 // 25 // Interface values are deeply copied if the underlying type can be 26 // deeply copied. 27 // 28 // There are a few exceptions that may result in a deeply copied value not 29 // deeply equal (asserted by DeepEqual(dst, src)) to the source value: 30 // 31 // 1) Func values are still refer to the same function 32 // 2) Chan values are replaced by newly created channels 33 // 3) One-way Chan values (receive or read-only) values are still refer 34 // to the same channel 35 // 36 // Note that while correct uses of DeepCopy do exist, they are not rare. 37 // The use of DeepCopy often indicates the copying object does not contain 38 // a singleton or is never meant to be copied, such as sync.Mutex, os.File, 39 // net.Conn, js.Value, etc. In these cases, the copied value retains the 40 // memory representations of the source value but may result in unexpected 41 // consequences in follow-up usage, the caller should clear these values 42 // depending on their usage context. 43 // 44 // To change these predefined behaviors, use provided DeepCopyOption. 45 func DeepCopy[T any](src T, opts ...DeepCopyOption) (dst T) 46 ``` 47 48 _Warning_: Not largely tested. Use it with care. 49 50 ## License 51 52 MIT | © 2022 The golang.design Initiative Authors, written by Changkun Ou.