Version: v0.0.0-...-49e55dd Latest Latest

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

Go to latest
Published: Jul 20, 2021 License: Apache-2.0 Imports: 4 Imported by: 0



Package compare contains the implementation of the compare plugin, which generates the deriveCompare function.

The deriveCompare function is a maintainable and fast way to implement fast Less functions.

deriveCompare(T, T) bool
deriveCompare(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 deriveCompare function will be found, because it was not implemented and it has a prefix deriveCompare. This prefix is configurable.

package main

import "sort"

type MyStruct struct {
	Int64     int64
	StringPtr *string

func sortStructs(ss []*MyStruct) {
	sort.Slice(ss,  func(i, j int) bool {
			return deriveCompare(ss[i], ss[j]) < 0

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:

This plugin has been tested thoroughly.



This section is empty.


This section is empty.


func New

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

func NewPlugin

func NewPlugin() derive.Plugin

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


This section is empty.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL