Somehow my error validations are duplicated when I use valid global variable like below
var (
valid validation.Validation
)
func validationInit() validation.Validation {
valid := validation.Validation{}
return valid
}
But when I move valid := validation.Validation{}
to my model function it works fine without any duplicates like below:
func AddClub(name string) (id int64, error []*validation.ValidationError) {
club := Club{Name: name}
valid := validation.Validation{}
How am I able to not duplicate this valid across each function but reuse variable without results being incremented and duplicated?
Since your validationInit()
func returns a validation.Validation
value and not a pointer to it, you cannot return the same global variable from multiple functions or from multiple invocations of the same function.
If you really want this, you have to return a pointer to the global variable, else a copy of the value of the global variable will be returned.
Example:
var valid validation.Validation
func someFunc() *valid.Validation {
// You can access the global variable valid here, you can also modify it
return &valid
}
But this is most likely not what you want. This would not allow you to have 2 different validation.Validation
values exist at the same time returned by your functions.
I suggest you to leave out the global variable, and just create a new validation.Validation
value each time it is needed and return that new value (either by value or a pointer to it).