0

I am not sure if this is the right term for this scenario.

I have a struct defined in a package, with a variable defined as type string, and I have a setter method to set the value of that variable. However, I cannot be sure of the type of value that could be passed to that method. It could be an int or a string. Is there a concept that is similar to java, where I can write the same function twice, but with different parameters, so that I don't have to do a type checking before populating the struct?

scott
  • 1,557
  • 3
  • 15
  • 31
  • 3
    No overloading in Go, well explained here: http://stackoverflow.com/questions/6986944 – fafl Jan 09 '16 at 08:44
  • 3
    No there is not. You need two functions, but then you don't need type checking. Stop thinking Java. – Volker Jan 09 '16 at 09:24
  • 2
    Well, *strictly* speaking, there isn't. To reduce redundant code however, you can accept an interface and do a [type switch](https://golang.org/doc/effective_go.html#type_switch). – Markus W Mahlberg Jan 09 '16 at 11:42

0 Answers0