diff --git a/src/reflect/type.go b/src/reflect/type.go index dd96105e..51c246ce 100644 --- a/src/reflect/type.go +++ b/src/reflect/type.go @@ -465,6 +465,9 @@ func (t *rawType) isNamed() bool { } func TypeOf(i interface{}) Type { + if i == nil { + return nil + } typecode, _ := decomposeInterface(i) return (*rawType)(typecode) } diff --git a/src/reflect/value_test.go b/src/reflect/value_test.go index 08bd325a..692c9a57 100644 --- a/src/reflect/value_test.go +++ b/src/reflect/value_test.go @@ -313,6 +313,14 @@ func TestAddr(t *testing.T) { } } +func TestNilType(t *testing.T) { + var a any = nil + typ := TypeOf(a) + if typ != nil { + t.Errorf("Type of any{nil} is not nil") + } +} + func equal[T comparable](a, b []T) bool { if len(a) != len(b) { return false