v0.0.0-...-886b66b Latest Latest

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

Go to latest
Published: Jul 21, 2022 License: Apache-2.0 Imports: 4 Imported by: 1



Package equal contains the implementation of the equal plugin, which generates the deriveEqual function.

The deriveEqual function is a faster alternative to reflect.DeepEqual.

deriveEqual(T, T) bool
deriveEqual(T) func(T) bool

When goderive walks over your code it is looking for a function that:

  • was not implemented (or was previously derived) and
  • has a predefined prefix.

In the following code the deriveEqual function will be found, because it was not implemented and it has a prefix deriveEqual. This prefix is configurable.

package main

type MyStruct struct {
	Int64     int64
	StringPtr *string

func (this *MyStruct) Equal(that *MyStruct) bool {
	return deriveEqual(this, that)

goderive will then generate the following code in a derived.gen.go file in the same package:

func deriveEqual(this, that *MyStruct) bool {
	return (this == nil && that == nil) ||
		this != nil && that != nil &&
		this.Int64 == that.Int64 &&
		((this.StringPtr == nil && that.StringPtr == nil) ||
			(this.StringPtr != nil && that.StringPtr != nil && *(this.StringPtr) == *(that.StringPtr)))

Supported types:

  • basic types
  • named structs
  • slices
  • maps
  • pointers to these types
  • private fields of structs in external packages (using reflect and unsafe)
  • and many more

Unsupported types:

  • chan
  • interface
  • function
  • unnamed structs, which are not comparable with the == operator

Example output can be found here: https://github.com/awalterschulze/goderive/tree/master/example/plugin/equal

This plugin has been tested thoroughly.



This section is empty.


This section is empty.


func New

New is a constructor for the equal code generator. This generator should be reconstructed for each package.

func NewPlugin

func NewPlugin() derive.Plugin

NewPlugin creates a new equal plugin. This function returns the plugin name, default prefix and a constructor for the equal code generator.


This section is empty.

Jump to

Keyboard shortcuts

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