Popularity
7.4
Stable
Activity
4.3
-
932
21
36

Programming language: Go

# goderive alternatives and similar packages

Based on the "Generation and Generics" category.
Alternatively, view goderive alternatives based on common mentions on social networks and blogs.

• ### GoWrap

GoWrap is a command line tool for generating decorators for Go interfaces
• ### go-enum

An enum generator for go
• ### Scout APM: A developer's best friend. Try free for 14-days

Scout APM uses tracing logic that ties bottlenecks to source code so you know the exact line of code causing performance issues and can get back to building a great product faster.
Promo
• ### goverter

Generate type-safe Go converters by simply defining an interface
• ### gotype

Golang source code parsing, usage like reflect package
• ### generis

Versatile Go code generator.
• ### go-xray

Helpers for making the use of reflection easier
• ### typeregistry

create type dynamically in Golang

Do you think we are missing an alternative of goderive or a related project?

## goderive

`goderive` derives mundane golang functions that you do not want to maintain and keeps them up to date.

It does this by parsing your go code for functions, which are not implemented, and then generates these functions for you by deriving their implementations from the input parameter types.

## Examples

In the following code the `deriveEqual` function will be spotted as a function that was not implemented (or was previously derived) and has a prefix `deriveEqual`.

``````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)))
}
``````

Recursive Examples:

Set Examples:

Functional Examples:

Concurrency Examples:

## Functions

Recursive Functions:

• Equal
• `deriveEqual(T, T) bool`
• `deriveEqual(T) func(T) bool`
• Compare
• `deriveCompare(T, T) int`
• `deriveCompare(T) func(T) int`
• DeepCopy
• `deriveDeepCopy(dst *T, src *T)`
• `deriveDeepCopy(dst []T, src []T)`
• `deriveDeepCopy(dst map[A]B, src map[A]B)`
• Clone `deriveClone(T) T`
• GoString `deriveGoString(T) string`
• Hash `deriveHash(T) uint64`

Set Functions:

• Keys `deriveKeys(map[K]V) []K`
• Sort `deriveSort([]T) []T`
• Unique `deriveUnique([]T) []T`
• Set `deriveSet([]T) map[T]struct{}`
• Min
• `deriveMin(list []T, default T) (min T)`
• `deriveMin(T, T) T`
• Max
• `deriveMax(list []T, default T) (max T)`
• `deriveMax(T, T) T`
• Contains `deriveContains([]T, T) bool`
• Intersect
• `deriveIntersect(a, b []T) []T`
• `deriveIntersect(a, b map[T]struct{}) map[T]struct{}`
• Union
• `deriveUnion(a, b []T) []T`
• `deriveUnion(a, b map[T]struct{}) map[T]struct{}`

Functional Functions:

• Fmap
• `deriveFmap(func(A) B, []A) []B`
• `deriveFmap(func(rune) B, string) []B`
• `deriveFmap(func(A) B, func() (A, error)) (B, error)`
• `deriveFmap(func(A) (B, error), func() (A, error)) (func() (B, error), error)`
• `deriveFmap(func(A), func() (A, error)) error`
• `deriveFmap(func(A) (B, c, d, ...), func() (A, error)) (func() (B, c, d, ...), error)`
• Join
• `deriveJoin([][]T) []T`
• `deriveJoin([]string) string`
• `deriveJoin(func() (T, error), error) func() (T, error)`
• `deriveJoin(func() (T, ..., error), error) func() (T, ..., error)`
• Filter `deriveFilter(pred func(T) bool, []T) []T`
• All `deriveAll(pred func(T) bool, []T) bool`
• Any `deriveAny(pred func(T) bool, []T) bool`
• TakeWhile `deriveTakeWhile(pred func(T) bool, []T) []T`
• Flip `deriveFlip(f func(A, B, ...) T) func(B, A, ...) T`
• Curry `deriveCurry(f func(A, B, ...) T) func(A) func(B, ...) T`
• Uncurry `deriveUncurry(f func(A) func(B, ...) T) func(A, B, ...) T`
• Tuple `deriveTuple(A, B, ...) func() (A, B, ...)`
• Compose
• `deriveCompose(func() (A, error), func(A) (B, error)) func() (B, error)`
• `deriveCompose(func(A) (B, error), func(B) (C, error)) func(A) (C, error)`
• `deriveCompose(func(A...) (B..., error), func(B...) (C..., error)) func(A...) (C..., error)`
• `deriveCompose(func(A...) (B..., error), ..., func(C...) (D..., error)) func(A...) (D..., error)`
• Mem
• `deriveMem(func(A...) (B...)) func(A...) (B...)`
• Traverse
• `deriveTraverse(func(A) (B, error), []A) ([]B, error)`
• ToError
• `deriveToError(error, func(A...) (B..., bool)) func(A...) (B..., error)`
• `deriveToError(error, func() bool) func() error`

Concurrency Functions:

• Fmap
• `deriveFmap(func(A) B, <-chan A) <-chan B`
• Join
• `deriveJoin(<-chan <-chan T) <-chan T`
• `deriveJoin(chan <-chan T) <-chan T`
• `deriveJoin([]<-chan T) <-chan T`
• `deriveJoin([]chan T) <-chan T`
• `deriveJoin(chan T, chan T, ...) <-chan T`
• Pipeline
• `derivePipeline(func(A) <-chan B, func(B) <-chan C) func(A) <-chan C`
• Do
• `deriveDo(func() (A, error), func (B, error)) (A, B, error)`
• Dup
• `deriveDup(c <-chan T) (c1, c2 <-chan T)`

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.

Functions which have been previously derived will be regenerated to keep them up to date with the latest modifications to your types. This keeps these functions, which are truly mundane to write, maintainable.

For example when someone in your team adds a new field to a struct and forgets to update the CopyTo method. This problem is solved by goderive, by generating generated functions given the new types.

Function prefixes are by default `deriveCamelCaseFunctionName`, for example `deriveEqual`. These are customizable using command line flags.

You can derive functions for different types by using different suffixes with the same prefix. For example, if you wish to derive `Equal` for types `MyStruct` and `MySecondStruct`, name the functions `deriveEqualMyStruct` and `deriveEqualMySecondStruct` and `goderive` will derive both.

Let `goderive` edit your function names in your source code, by enabling `autoname` and `dedup` using the command line flags. These flags respectively make sure that your functions have unique names and that you don't generate multiple functions that do the same thing.

## How to run

goderive can be run from the command line:

`goderive ./...`

, using the same path semantics as the go tool.

And you can customize specific function prefixes

Or you can customize all function prefixes

You can let goderive rename your functions using the `-autoname` and `-dedup` flags. If these flags are not used, goderive will not touch your code and rather return an error.

## Customization

The derive package allows you to create your own code generator plugins, see all the current plugins for examples.

You can also create your own vanity binary. Including your own generators and/or customization of function prefixes, etc. This should be easy to figure out by looking at main.go

## Users

These projects use goderive:

Please let us know if you are using goderive by opening an issue or a pull request that adds your project to the list.

## Mentioned

Please let us know if you mention goderive in a blog post, talk or go experience report, so that we can add a link to our list.
{"ft_lang_label":"__label__en","ft_lang_prob":0.62762743,"math_prob":0.8541276,"size":7235,"snap":"2021-43-2021-49","text_gpt3_token_len":1993,"char_repetition_ratio":0.18102613,"word_repetition_ratio":0.016559338,"special_character_ratio":0.27436075,"punctuation_ratio":0.2013889,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97425896,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-23T07:55:39Z\",\"WARC-Record-ID\":\"<urn:uuid:226e76b0-0475-453e-a3cc-2a136efd76ac>\",\"Content-Length\":\"47739\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1c33d8b1-5e57-4dc8-8b96-7b2dd2c71192>\",\"WARC-Concurrent-To\":\"<urn:uuid:88d53694-a233-404f-ab84-6fc1922f18a1>\",\"WARC-IP-Address\":\"172.66.41.34\",\"WARC-Target-URI\":\"https://go.libhunt.com/goderive-alternatives\",\"WARC-Payload-Digest\":\"sha1:KK7NBF7YNHQNENNPHQ3WIYR6W4MFMSHZ\",\"WARC-Block-Digest\":\"sha1:V47EAI5J2AQONU6NFHFXDYN3E6GYOFLK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585653.49_warc_CC-MAIN-20211023064718-20211023094718-00122.warc.gz\"}"}