Skip to content

Commit

Permalink
check for pointers before call NumFields
Browse files Browse the repository at this point in the history
  • Loading branch information
neftales committed Jun 1, 2024
1 parent 8d5b23a commit 3a17d04
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions reflection/reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import "reflect"
func walk(x interface{}, fn func(input string)) {
val := reflect.ValueOf(x)

if val.Kind() == reflect.Pointer {
val = val.Elem()
}

for i := 0; i < val.NumField(); i++ {
field := val.Field(i)

if field.Kind() == reflect.String {
switch field.Kind() {
case reflect.String:
fn(field.String())
}

if field.Kind() == reflect.Struct {
case reflect.Struct:
walk(field.Interface(), fn)
}
}
Expand Down

0 comments on commit 3a17d04

Please sign in to comment.