Method overloading alternative Go

My apologies if this is not the right place for this kind of questions.

Coming from a java background I’m uncertain what the best way would be to call different functions on the basis of the struct given as an argument to it. In Java, I would create several methods and the right one would get executed because of the method overloading, what would be the best way to solve this in Go taking in account new structs will be added later on with the need for new functions accordingly?

If possible, you should change the argument into a method receiver. This should work as long as there aren’t two different objects involved that can both vary in type (and all the types involved are ones that were defined in packages that you control).

Otherwise, you’ll probably need to use a type switch: https://golang.org/doc/effective_go.html#type_switch

2 Likes

Thank you very much, this would certainly work.

I would use methods with different names, at least most of the time.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.