From 1aa708f9a89f9d552c8dd4dbbfabedf897eb6bf2 Mon Sep 17 00:00:00 2001 From: andyning Date: Thu, 21 Nov 2019 23:02:35 +0800 Subject: [PATCH] fix the bug reflect on zero Value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix the bug :reflect: call of reflect.Value.Type on zero Value if the slice is nil, or len==0, will be panic. the case: type Foo struct{ Bar[]int } func TestGoFunk(t *testing.T) { Log.InitLog() foo := []Foo{} bars := funk.Get(foo, "Bar") //panic !! fmt.Print(bars) } --- retrieve.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/retrieve.go b/retrieve.go index e1daeb8..e2fe751 100644 --- a/retrieve.go +++ b/retrieve.go @@ -21,7 +21,11 @@ func get(value reflect.Value, path string) reflect.Value { var resultSlice reflect.Value length := value.Len() - + + if length == 0 { + return resultSlice + } + for i := 0; i < length; i++ { item := value.Index(i)