How to make, what one function and the same could accept different type of parameters? for example, if it receives an incoming int parameter, one is done, if the string is something else. eg

func myfunc(??? ){ //ΠΊΠ°ΠΊΠΎΠ΅-Π½ΠΈΠ±ΡƒΠ΄ΡŒ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΠΎΠ΅ дСйствиС для ΠΎΠ±ΠΎΠΈΡ… Π²Π°Ρ€ΠΈΠ°Π½Ρ‚ΠΎΠ² log.Println("МнС ΠΏΠ΅Ρ€Π΅Π΄Π°Π»ΠΈ число,ΠΈΠ΄Ρ‘ΠΌ ΠΏΠΎ ΠΏΡƒΡ‚ΠΈ 1")//ΠΈΠ»ΠΈ... log.Println("МнС ΠΏΠ΅Ρ€Π΅Π΄Π°Π»ΠΈ строку,ΠΈΠ΄Ρ‘ΠΌ ΠΏΠΎ ΠΏΡƒΡ‚ΠΈ 2") } 

Or do you need to write 2 different functions?

    1 answer 1

    Through the empty interface and a switch on type :

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

    http://play.golang.org/p/EUmI1N2OYp .