exp

package
v0.2.12 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 4 Imported by: 0

README

About DeepCopy

This feature is experimental. Please avoid to import this package in critical code.

Currently, DeepCopy cannot correctly handle composite types with circular references. For example, the following case:

package x

type T struct {
	pointer *T
	slice   []T
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone

func Clone[T any](src T) (dst T)

func DeepCopy

func DeepCopy(dst, src reflect.Value)

DeepCopy performs a deep copy from the source reflect.Value `src` to the destination reflect.Value `dst`.

It recursively copies all fields, including unexported ones, and supports structs, slices, maps, pointers, arrays, and interfaces. It does not support functions or channels.

func HackField

func HackField(v any, i int) reflect.Value

HackField returns an addressable reflect.Value for the struct field at the given index, even if the field is unexported (private).

Like HackFieldByName, this function uses unsafe.Pointer to bypass Go's visibility restrictions. It should be used with caution, as it breaks encapsulation and may be incompatible with future Go versions.

func HackFieldByName

func HackFieldByName(v any, name string) reflect.Value

HackFieldByName returns an addressable `reflect.Value` for the specified field name of a struct, even if the field is unexported (private).

This function uses unsafe.Pointer to bypass Go's visibility restrictions, allowing direct access to unexported struct fields. It should be used with caution, as it breaks encapsulation and may not be compatible with future versions of Go.

Example:

type Foo struct { bar int }
f := Foo{bar: 42}
v := HackFieldByName(f, "bar")
v.SetInt(100)  // Successfully modifies the unexported field

Types

This section is empty.

Jump to

Keyboard shortcuts

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