Answer the question
In order to leave comments, you need to log in
How to create an overloaded function in golang?
How to make one function and the same one could take different types of parameters?
for example, if it receives an incoming int parameter, then one thing is done, if a string is something else.
For example
func myfunc(??? ){
//какое-нибудь одинаковое действие для обоих вариантов
log.Println("Мне передали число,идём по пути 1")//или...
log.Println("Мне передали строку,идём по пути 2")
}
Answer the question
In order to leave comments, you need to log in
Solved play.golang.org/p/EUmI1N2OYp
func f(v interface{}) {
switch v.(type) {
case int:
fmt.Println("int", v)
case string:
fmt.Println("string", v)
default:
panic(fmt.Sprintf("f: unsupported type %T", v))
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question