I am having trouble understanding why type switches are written with an additional variable defined in the switch statement. The below code seems to be the sanctioned way of doing things:
func test_func(i interface{}) {
switch v := i.(type) {
case int:
fmt.Printf("%T
", v)
case float64:
fmt.Printf("%T
", v)
case int:
fmt.Printf("I don't know about type %T!
", v)
}
}
func main() {
test_func(float64(34))
test_func(int(34))
test_func("hello world")
}
As expected, this returns:
float64
int
I don't know about type string!
However, I can change test_func
slightly so that v
is not defined in the switch
statement, and instead we use i
inside our case statements:
func test_func(i interface{}) {
switch i.(type) {
case int:
fmt.Printf("%T
", i)
case float64:
fmt.Printf("%T
", i)
case int:
fmt.Printf("I don't know about type %T!
", i)
}
}
func main() {
test_func(float64(34))
test_func(int(34))
test_func("hello world")
}
And the output is not changed. It seems the two forms are interchangeable. Why would I go to the trouble of defining v
when I could just use i
? The latter case is simpler since there is one less variable to keep track of; maybe it is even more performant.