copycat

package module
v0.0.0-...-8835af4 Latest Latest
Warning

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

Go to latest
Published: May 15, 2021 License: MIT Imports: 1 Imported by: 0

README

copycat

GoDoc Go Coverage Status Go Report Card

Recursively perform deep copy from source (src) to destination (dst) using reflection until either end got exhausted. Support array, slice, struct, pointer and interface. Copying between different primitive types are tolerated.

Getting started

To get the package, execute:

go get github.com/maxtong1987/copycat

To import the package, add the following line to your code:

import "github.com/maxtong1987/copycat"

Example

package main

import (
	"fmt"

	"github.com/maxtong1987/copycat"
)

type subType struct {
	X string
	Y bool
	Z int
}

type srcType struct {
	A string
	B int32
	C float64
	D []uint64
	E subType
}

type destType struct {
	A string
	B int64
	C float32
	d []uint64
	E *subType
}

func (d *destType) String() string {
	return fmt.Sprintf("A:%s B:%v C:%v d:%v E:%+v", d.A, d.B, d.C, d.d, *d.E)
}

func main() {
	src := srcType{
		A: "a",
		B: 123,
		C: 0.0122,
		D: []uint64{6, 7, 8, 9},
		E: subType{X: "x", Y: true, Z: 100},
	}
	dst := &destType{}
	copycat.DeepCopy(dst, src)
	fmt.Print(dst)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepCopy

func DeepCopy(dst interface{}, src interface{}) error

DeepCopy recursively copies data from src to dst.

Types

This section is empty.

Jump to

Keyboard shortcuts

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