reflect

package
v0.262.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package reflect implements the proposal https://go.dev/issue/51520.

Warning: Not largely tested. Use it with care. nolint

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepCopy

func DeepCopy[T any](src T, opts ...DeepCopyOption) (dst T)

DeepCopy copies src to dst recursively.

Two values of identical type are deeply copied if one of the following cases apply.

Numbers, bools, strings are deeply copied and have different underlying memory address.

Slice and Array values are deeply copied, including its elements.

Map values are deeply copied for all of its key and corresponding values.

Pointer values are deeply copied for their pointed value, and the pointer points to the deeply copied value.

Struct values are deeply copied for all fields, including exported and unexported.

Interface values are deeply copied if the underlying type can be deeply copied.

There are a few exceptions that may result in a deeply copied value not deeply equal (asserted by DeepEqual(dst, src)) to the source value:

  1. Func values are still refer to the same function
  2. Chan values are replaced by newly created channels
  3. One-way Chan values (receive or read-only) values are still refer to the same channel

Note that while correct uses of DeepCopy do exist, they are not rare. The use of DeepCopy often indicates the copying object does not contain a singleton or is never meant to be copied, such as sync.Mutex, os.File, net.Conn, js.Value, etc. In these cases, the copied value retains the memory representations of the source value but may result in unexpected consequences in follow-up usage, the caller should clear these values depending on their usage context.

To change these predefined behaviors, use provided DeepCopyOption.

Types

type DeepCopyOption

type DeepCopyOption func(opt *copyConfig)

DeepCopyOption represents an option to customize deep copied results.

func DisallowCopyBidirectionalChan

func DisallowCopyBidirectionalChan() DeepCopyOption

DisallowCopyBidirectionalChan returns a DeepCopyOption that disables the behavior of producing new channel when a bidirectional channel is copied.

func DisallowCopyCircular

func DisallowCopyCircular() DeepCopyOption

DisallowCopyCircular returns a DeepCopyOption that disables the behavior of copying circular structures.

func DisallowCopyUnexported

func DisallowCopyUnexported() DeepCopyOption

DisallowCopyUnexported returns a DeepCopyOption that disables the behavior of copying unexported fields.

func DisallowTypes

func DisallowTypes(val ...any) DeepCopyOption

DisallowTypes returns a DeepCopyOption that disallows copying any types that are in given values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL