Given the codes
type Int struct {
v int
}
func typeAssert(val any) {
switch v := val.(type) {
case nil:
fmt.Println("val type is nil")
case *Int:
fmt.Println("val type is *Int")
if v == nil {
fmt.Println("val after type assertion is nil?")
}
}
}
func main() {
var i1 *Int
typeAssert(i1)
}
Output:
val type is *Int
val after type assertion is nil?
What confuses me is that since the *Int
is matched in switch v := val.(type)
, why could the v == nil
be true
? If the v == nil
be true
, the case nil
could be matched, actually, it does not.