Answer the question
In order to leave comments, you need to log in
How to pass arrays of structures of different types to a function in Golang?
I have several arrays of structures of different types.
I need to pass these arrays one by one to the function.
But the types of structures are different.
Code something like this:
func Function(MyStruct /*Что здесь написать?*/){
for _ , field := range MyStruct {
...
}
}
func main() {
...
Function(city, country, []MyStruct1)
Function(city, country, []MyStruct2)
...
}
Answer the question
In order to leave comments, you need to log in
Do not do it this way.
You don't need to pull into a typed language that protects you from your own mistakes - you don't need to pull architectural principles from dynamic languages into it.
In the forehead: it can be replaced with an iterator interface, the specific implementation of which will be different for different types, but the signature of the interface will be the same, so it can be easily passed to a function.
Less writing, but ugly - passing arrays of different types in different parameters
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question