From 80264754240d6568140934053a2c011ddd0ab5a8 Mon Sep 17 00:00:00 2001 From: Muyao CHEN Date: Fri, 20 Sep 2024 20:05:32 +0200 Subject: [PATCH] walk: check if the field is of string type --- walk/walk.go | 4 +++- walk/walk_test.go | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/walk/walk.go b/walk/walk.go index c3125f6..1994b84 100644 --- a/walk/walk.go +++ b/walk/walk.go @@ -7,6 +7,8 @@ import ( func Walk(x interface{}, fn func(string)) { val := reflect.ValueOf(x) for i := 0; i < val.NumField(); i++ { - fn(val.Field(i).String()) + if val.Field(i).Kind() == reflect.String { + fn(val.Field(i).String()) + } } } diff --git a/walk/walk_test.go b/walk/walk_test.go index 1ded3e4..d7aa56b 100644 --- a/walk/walk_test.go +++ b/walk/walk_test.go @@ -26,6 +26,14 @@ func TestWalk(t *testing.T) { }{"Chris", "London"}, []string{"Chris", "London"}, }, + { + "struct with non string field", + struct { + Name string + Age int + }{"Chris", 29}, + []string{"Chris"}, + }, } for _, test := range cases {