The deriveContains function returns whether an element exists in a list.
Given the following input:
package contains
import (
"fmt"
)
type boat struct {
id int
}
func appendUnique(olds, news []boat) ([]boat, error) {
for i := range news {
if deriveContains(olds, news[i]) {
return nil, fmt.Errorf("duplicate found")
}
}
return append(olds, news...), nil
}
goderive will generate the following code:
// Code generated by goderive DO NOT EDIT.
package contains
// deriveContains returns whether the item is contained in the list.
func deriveContains(list []boat, item boat) bool {
for _, v := range list {
if v == item {
return true
}
}
return false
}