2 things: 1. plus() returns interface{}.
"%T" prints underlying dynamic type of interface{} value, but static type of returned value of plus is interface{} You can assign any type to interface{} without a cast by definition (see e.g. https://www.programming-books.io/essential/go/a-90100072-empty-interface) What it means is this is valid in Go: var v interface{} v = 5 // no cast needed v = "foo" // no cast needed interface{} is Go's version of dynamic type. It wraps any type as a tuple (type, value). 2. Let's expand plus() function: func plus(a, b interface{}) interface{} { aInt := a.(myInt) // type: myInt bInt := b.(myInt) // type: myInt res := aInt + bInt // type: myInt var ires interface{} = res; // no cast needed, see above return ires } Hope this clarifies the typing. Aside: if you come from C++, aliasing interface{} as any might feel comfortable, but that's not a good Go style. On Saturday, February 17, 2018 at 9:03:58 PM UTC-8, Bill Wood wrote: > > HI, Go newbie here... not sure if this is a dumb question or not :) > > I have a simple program: > > package main > > import "fmt" > > type any interface{} > type myInt int > > func plus(a, b any) any { return a.(myInt) + b.(myInt) } > > func main() { > s := plus(myInt(3), myInt(2)) > fmt.Printf("%v, %T\n", s, s) > } > > > Output: > > 5, main.myInt > > > Why does func plus return a myInt? Why isn't a cast needed, ie: > > func plus(a, b any) any { return myInt(a.(myInt) + b.(myInt)) } > > > Thanks! > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.