From c3728b75024667927f690373cea324dd26777414 Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Tue, 30 Aug 2022 01:30:53 +0800 Subject: [PATCH] reflect: simplify Value.Comparable using Type.Comparable to simplify the Value.Comparable, and return true directly when exit the for loop of kind == array and elements type is interface or array or struct. Change-Id: Ib0b06a70642ba24c9215c69e7d619960fbeeed90 Reviewed-on: https://go-review.googlesource.com/c/go/+/426457 Auto-Submit: Ian Lance Taylor Reviewed-by: Dmitri Shuralyov Run-TryBot: Ian Lance Taylor TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor Run-TryBot: xie cui <523516579@qq.com> --- src/reflect/value.go | 29 ++--------------------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/src/reflect/value.go b/src/reflect/value.go index 4456fdc5a5..ae6c4daf58 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -3270,14 +3270,6 @@ func (v Value) Comparable() bool { case Invalid: return false - case Bool, - Int, Int8, Int16, Int32, Int64, - Uint, Uint8, Uint16, Uint32, Uint64, - Uintptr, - Float32, Float64, Complex64, Complex128, - Chan: - return true - case Array: switch v.Type().Elem().Kind() { case Interface, Array, Struct: @@ -3286,27 +3278,13 @@ func (v Value) Comparable() bool { return false } } + return true } return v.Type().Comparable() - case Func: - return false - case Interface: return v.Elem().Comparable() - case Map: - return false - - case Pointer: - return true - - case Slice: - return false - - case String: - return true - case Struct: for i := 0; i < v.NumField(); i++ { if !v.Field(i).Comparable() { @@ -3315,11 +3293,8 @@ func (v Value) Comparable() bool { } return true - case UnsafePointer: - return true - default: - return false + return v.Type().Comparable() } }