Coder Social home page Coder Social logo

go-deepcopy's Introduction

Go Deep Copy

Build Status codecov

This package is a Golang implementation for creating deep copies of virtually any kind of Go type.

This is a truly deep copy--every single value behind a pointer, every item in a slice or array, and every key and value in a map are all cloned so nothing is pointing to what it pointed to before.

To handle circular pointer references (e.g. a pointer to a struct with a pointer field that points back to the original struct), we keep track of a map of pointers that have already been visited. This serves two purposes. First, it keeps us from getting into any kind of infinite loop. Second, it ensures that the code will behave similarly to how it would have on the original struct -- if you expect two values to be pointing at the same value within the copied tree, then they'll both still point to the same thing.

Sample Program

package main

import (
	"fmt"

	"github.com/barkimedes/go-deepcopy"
)

type Foo struct {
	Bar []string
	Baz *Baz
}

type Baz struct {
	Qux   int
	Corgi map[bool]string
	Foo   *Foo
}

func main() {
	x := &Foo{
		Bar: []string{"a", "b", "c", "d"},
		Baz: &Baz{
			Qux: 4,
			Corgi: map[bool]string{
				false: "nope",
				true:  "yup",
			},
		},
	}

	x.Baz.Foo = x // just for funsies

	y, err := deepcopy.Anything(x)
	if err != nil {
		panic(err)
	}
	print(x)
	fmt.Println()
	print(y.(*Foo))
}

func print(x *Foo) {
	fmt.Printf("Foo: %p %v\n", x, x)
	fmt.Printf("\tFoo.Bar: %p %v\n", x.Bar, x.Bar)
	fmt.Printf("\tFoo.Baz: %p %v\n", x.Baz, x.Baz)
	fmt.Printf("\t\tFoo.Baz.Qux: %v\n", x.Baz.Qux)
	fmt.Printf("\t\tFoo.Baz.Corgi: %p %v\n", x.Baz.Corgi, x.Baz.Corgi)
	fmt.Printf("\t\tFoo.Baz.Foo: %p %v\n", x.Baz.Foo, x.Baz.Foo)
}

Sample Output

Note that the values are all the same, but the addresses are all different. Note also that circular dependencies are handled--the self-referential Foo remains self-referential within each instance, but different across copies.

Foo: 0xc00000c0a0 &{[a b c d] 0xc00000c0c0}
	Foo.Bar: 0xc000016080 [a b c d]
	Foo.Baz: 0xc00000c0c0 &{4 map[false:nope true:yup] 0xc00000c0a0}
		Foo.Baz.Qux: 4
		Foo.Baz.Corgi: 0xc000060180 map[false:nope true:yup]
		Foo.Baz.Foo: 0xc00000c0a0 &{[a b c d] 0xc00000c0c0}

Foo: 0xc00000c0e0 &{[a b c d] 0xc00000c160}
	Foo.Bar: 0xc0000160c0 [a b c d]
	Foo.Baz: 0xc00000c160 &{4 map[false:nope true:yup] 0xc00000c0e0}
		Foo.Baz.Qux: 4
		Foo.Baz.Corgi: 0xc0000601e0 map[false:nope true:yup]
		Foo.Baz.Foo: 0xc00000c0e0 &{[a b c d] 0xc00000c160}

go-deepcopy's People

Contributors

barkimedes avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.