Skip to content

qdm12/reprint

Repository files navigation

Reprint

Reprint is a Go library to deep copy any object THE RIGHT WAY ™️

reprint

Join Slack channel Build Status

GitHub last commit GitHub commit activity GitHub issues

Features

Unlike most libraries out there, this one deep copies by assigning new pointers to all data structures nested in a given object, hence doing it THE RIGHT WAY ™️

It works with slices, arrays, maps, pointers, nested pointers, nils, structs (with unexported fields too), functions and channels.

Limits:

  • Functions pointers are not changed but that's by design
  • Channels buffered elements are not deep copied

Usage

go get -u github.com/qdm12/reprint

You can check out Golang Playground and activate Imports at the top, or read this:

package main

import (
    "fmt"

    "github.com/qdm12/reprint"
)

func main() {
    one := 1
    two := 2
    type myType struct{ A *int }

    // reprint.FromTo usage:
    var x, y myType
    x.A = &one
    reprint.FromTo(&x, &y) // you can check the error returned also
    y.A = &two
    fmt.Println(x.A, *x.A) // 0xc0000a0010 1
    fmt.Println(y.A, *y.A) // 0xc0000a0018 2

    // reprint.This usage:
    x2 := myType{&one}
    out := reprint.This(x2)
    y2 := out.(myType)
    y2.A = &two
    fmt.Println(x2.A, *x2.A) // 0xc0000a0010 1
    fmt.Println(y2.A, *y2.A) // 0xc0000a0018 2
}

Development

  1. Install Docker
    • On Windows, share a drive with Docker Desktop and have the project on that partition
    • On OSX, share your project directory with Docker Desktop
  2. With Visual Studio Code, install the remote containers extension
  3. In Visual Studio Code, press on F1 and select Remote-Containers: Open Folder in Container...
  4. Your dev environment is ready to go!... and it's running in a container 👍

TODOs

  • (Research) deep copy elements currently in channel
    • Race conditions
    • Pause channel?
  • Polish FromTo
    • Initialize copy to copy's type if it's a typed nil
    • Initialize copy to original's type if it's an untyped nil
    • Returns typed nil instead of untyped nil if original is a nil pointer (typed)
  • forceCopyValue might not be needed