golang multiple case in type switch

This is normal behaviour that is defined by the spec (emphasis mine):

The TypeSwitchGuard may include a short variable declaration. When that form is used, the variable is declared at the beginning of the implicit block in each clause. In clauses with a case listing exactly one type, the variable has that type; otherwise, the variable has the type of the expression in the TypeSwitchGuard.

So, in fact, the type switch does take effect, but the variable a keeps the type interface{}.

One way you could get around this is to assert that foo has the method test(), which would look something like this:

package main

import (
    "fmt"
)

type A struct {
    a int
}

func (this *A) test() {
    fmt.Println(this)
}

type B struct {
    A
}

type tester interface {
    test()
}

func main() {
    var foo interface{}
    foo = &B{}
    if a, ok := foo.(tester); ok {
        fmt.Println("foo has test() method")
        a.test()
    }
}

Leave a Comment