There is an existing logic I need to achieve in Golang. In general it is something like:
// Function receives an object with a variable type (struct)
func(dynObject interface{}) {
var variableFunc ... // What should be here?
// Some things to do depending on the input param type
switch dynObject.(type) {
case *objA:
// do some job
case *objB:
// do some job
}
// ...
// Creating and calculating some variables for the following loop
// ...
humans := []*Humans{ ....... }
sum := []int{}
for _, human := range humans {
result := variableFunc(dynObject, human)
sum = append(sum, result)
}
}
As you see - as an input there is a dynObject which is a struct, but can be any of some predefined structs. Later there is a function variableFunc
which must take this object and process, and processing logic also depends on the type. So type objA requires different logic than objB.
I don't know a normal way to implement this. How do I create this dynamic function, which can take different structs as input and apply needed logic to it? At the moment I get an error, that function expects another type as an input. And I wish to avoid additional switch-case, because one already exists.
Of course I tried to create a predefined func., and with interfaces, then somehow redefine it inside of existing switch-case, but no luck. I need your help