J
J
Julia Kovalenko2017-04-07 16:44:16
go
Julia Kovalenko, 2017-04-07 16:44:16

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

Is this even possible to implement?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dinegnet, 2017-04-07
@dinegnet

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

R
rustler2000, 2017-04-07
@rustler2000

[]interface{}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question