Hi
Why does the type of a parameter passed to another function changes?
For example:
func P(args ...interface{}) {
print("\n", args)
}
func print(seperator string, args ...interface{}) {
for _, arg := range args {
fmt.Println("arg type: ", reflect.TypeOf(arg))
str := ""
switch v := arg.(type) {
case byte:
str = string(v)
case rune:
str = string(v)
case int:
str = strconv.Itoa(v)
case int64:
str = strconv.FormatInt(v, 10)
case string:
str = v
}
out.WriteString(str + seperator)
}
}
Calling P("one"[2]) gives type as []interface {}
where as directly calling
print("one"[2]) gives type as
byte
Why is it like this?
And any way i can achieve this without putting switch in P() function?