Documentation
¶
Overview ¶
Package gocopy generates CopyFrom and Copy methods that make a struct value an independent copy of another without allocating what the destination already owns.
A generated CopyFrom(src) is one bulk assignment (*dst = *src) followed by fixes for the fields that would otherwise share memory: slices are copied into dst's own backing arrays (reusing their capacity), maps entry by entry into dst's own map, nested values with a CopyFrom through it. Pointers, interfaces, channels and funcs are assigned as they are. Struct tags choose a different treatment per field:
Buf []byte `copy:"-"` // dst keeps its own value Table []byte `copy:"shallow"` // assigned as it is, on purpose
A struct holding a lock or an atomic (also nested, by value) cannot be assigned as a whole: such fields must be tagged, the type is copied field by field, and it gets no Copy method.
The usual way to run it is a go:generate directive next to the type:
//go:generate go tool gocopy -type=Request
which writes request_copy.gen.go in the same package. Load and Package.Generate expose the generator to other tools.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNoPackage = errors.New("gocopy: no package found")
ErrNoPackage is returned by Load when dir holds no Go package.
Functions ¶
Types ¶
type Package ¶
type Package struct {
// contains filtered or unexported fields
}
Package is a loaded Go package whose struct types Generate can describe.
func Load ¶
Load type-checks the package in dir (the current directory when empty).
The dependencies come as export data from `go list -export`; the package itself is checked from source and its own type errors are tolerated, so a stale generated file (one that names a field that no longer exists) does not stop the type from being generated again. A dependency that does not compile is an error.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gocopy
command
Command gocopy writes a CopyFrom and Copy methods for the struct types named by -type into <type>_copy.gen.go in the current package.
|
Command gocopy writes a CopyFrom and Copy methods for the struct types named by -type into <type>_copy.gen.go in the current package. |
|
internal
|
|
|
cases/bulk
Package bulk is a gocopy test case: a struct without locks, copied in one assignment with the sharing fields fixed after it.
|
Package bulk is a gocopy test case: a struct without locks, copied in one assignment with the sharing fields fixed after it. |
|
cases/consts
Package consts holds a type a copied map refers to, so the generated code needs an import for it.
|
Package consts holds a type a copied map refers to, so the generated code needs an import for it. |
|
cases/fieldwise
Package fieldwise is a gocopy test case: a struct that cannot be assigned as a whole (it holds a lock, an atomic and a struct with a lock), so every field is copied on its own and there is no Copy method.
|
Package fieldwise is a gocopy test case: a struct that cannot be assigned as a whole (it holds a lock, an atomic and a struct with a lock), so every field is copied on its own and there is no Copy method. |