From 1c3ad9b9d86a2477f908179313df91f91876a8e3 Mon Sep 17 00:00:00 2001 From: Muyao CHEN Date: Fri, 20 Sep 2024 20:32:37 +0200 Subject: [PATCH] walk: refacto --- walk/walk.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/walk/walk.go b/walk/walk.go index 923f223..10777d7 100644 --- a/walk/walk.go +++ b/walk/walk.go @@ -7,22 +7,18 @@ import ( func Walk(x interface{}, fn func(string)) { val := getValue(x) - for i := 0; i < val.NumField(); i++ { - field := val.Field(i) - - if val.Kind() == reflect.Slice { - for i := 0; i < val.Len(); i++ { - Walk(val.Index(i).Interface(), fn) - } - } - - switch field.Kind() { - case reflect.String: - fn(field.String()) - case reflect.Struct: + switch val.Kind() { + case reflect.Struct: + for i := 0; i < val.NumField(); i++ { // XXX: Interface() to get interface - Walk(field.Interface(), fn) + Walk(val.Field(i).Interface(), fn) } + case reflect.Slice: + for i := 0; i < val.Len(); i++ { + Walk(val.Index(i).Interface(), fn) + } + case reflect.String: + fn(val.String()) } }