clone

package module
v0.0.0-...-735b540 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2023 License: MIT Imports: 5 Imported by: 0

README

go-clone

GoDoc GitHub go.mod Go version (subdirectory of monorepo) Go Report Card Coverage Status Build Status Release GitHub

This project is based on the Go language development cloning toolkit and supports shallow and deep copy operations of all types of data in the Go language.

When deep or shallow copy struct type data, you can manipulate exported or unexported fields.

Feature

  • Provides shallow and deep copy functions.
  • Comprehensive,efficient and reusable,supports shallow and deep copies for struct exported and unexported fields.
  • Does not depends on any third-party libraries.
  • Provide unit test cases for each exported functions.

Usage

Install:

go get "github.com/lmlat/go-clone"

Import:

import "github.com/lmlat/go-clone"

Note:

After importing the package, by default all provided functionality is defined in a package named clone.

Example

Deep Copy

Deep copy the value of a struct type, containing the fields in the struct that are not exported.

type A struct {
	name     string
	Age      int
	birthday time.Time
	hobby    []string
}

src := A{"aitao", 100, time.Now(), []string{"ping pong", "badminton", "football"}}
dst := Deep(src).(A)
// When you modify the value of non-reference type, the source data is not affected.
dst.name = "哆啦A梦" 
// When you modify the value of reference type, the source data is not affected.
dst.hobby[0] = "乒乓球"

Deep copy the value of a struct pointer type, containing the fields in the strcut that are not exported.

src := &A{"aitao", 100, time.Now(), []string{"ping pong", "badminton", "football"}}
dst := Deep(src).(*A) // deep copy value

Skip copying the specified fields in the struct via the 'ignore' tag:

type B struct {
    name     string `ignore:"name"`
	Age      int
	birthday time.Time
	hobby    []string
}

src := &B{"aitao", 100, time.Now(), []string{"ping pong", "badminton", "football"}}
dst := Deep(src).(*B)

Deep copy only the exported fields in the structure:

dst = Deep(src, WithOpFlags(OnlyPublicField)).(A)
// The same effect can be achieved using CopyProperties function.
dst = CopyProperties(src).(C)

Some commonly used operation flags:

  1. OnlyPublicField:only exported field values in the struct are copied, only struct type values.
  2. OnlyPrivateField:only field values that are not exported in the struct are copied, only struct type values.
  3. AllFields:copy all field values in the struct, including exported and unexported fields, only for struct type values.
  4. DeepString:deep copy string type values.
  5. DeepFunc:deep copy func type values.
  6. DeepArray:deep copy array type values.

Some special types of shallow copy processing:

  1. time.Time
  2. reflect.Type:The type system of the Go language is static at compile time, meaning that the type information is determined at compile time. Therefore, when dealing with the 'reflect.Type' interface, it is usually not necessary to make a deep copy (' reflect.rtype 'is immutable). Shallow copy the value of a struct type, containing the fields in the struct that are not exported.
Shallow Copy
src := C{"aitao", 100, time.Now(), []string{"ping pong", "badminton", "football"}}
dst := Shallow(src).(C)

Documentation

Index

Constants

View Source
const Ignore = "ignore"

Variables

This section is empty.

Functions

func CopyProperties

func CopyProperties(src any) (dst any)

CopyProperties only all exported fields in the struct type are copied.

func Deep

func Deep(src any, options ...Options) (dst any)

Deep returns the copy value after a deep-cloned source object.

func Shallow

func Shallow(src any) (dst any)

Shallow returns the copy value after a shallow-cloned source object.

Types

type Cloneable

type Cloneable interface {
	// Clone shallow clone creates a new object and copies the field values of the source object to the new object.
	//
	// If the source object contains reference type data, the shallow clone copies only the reference, not the reference object itself.
	//
	// That is, the source and clone objects will share the same reference object (only the top-level structure of the
	// object is copied, while the nested object is still a reference to the original object)
	Clone() any

	// DeepClone deep cloning creates a new object, which recursively copies the field values of the source object and all its
	// nested objects, ensuring that the cloned object is completely independent and does not have any shared reference objects.
	DeepClone() any
}

type Context

type Context struct {
	// contains filtered or unexported fields
}

type Empty

type Empty struct{}

type OpFlags

type OpFlags uint
const (
	Invalid          OpFlags = iota
	OnlyPublicField  OpFlags = 1 << (iota - 1) // only copy the exported fields.
	OnlyPrivateField                           // only copy fields are not exported.
	DeepString
	DeepFunc
	DeepArray
	AllFields = OnlyPublicField | OnlyPrivateField // copy all fields.
)

func (*OpFlags) Add

func (o *OpFlags) Add(flags OpFlags) OpFlags

func (*OpFlags) Clear

func (o *OpFlags) Clear(flags OpFlags) OpFlags

func (*OpFlags) Has

func (o *OpFlags) Has(flags OpFlags) bool

type Options

type Options func(*Context)

func AddOpFlags

func AddOpFlags(flags OpFlags) Options

func WithEnableCache

func WithEnableCache(enableCache bool) Options

func WithOpFlags

func WithOpFlags(flags OpFlags) Options

func WithTypes

func WithTypes(types ...reflect.Type) Options

Jump to

Keyboard shortcuts

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