walk: loop on the string fields
This commit is contained in:
parent
766d7449e8
commit
122770ae5c
@ -6,6 +6,7 @@ import (
|
||||
|
||||
func Walk(x interface{}, fn func(string)) {
|
||||
val := reflect.ValueOf(x)
|
||||
field := val.Field(0)
|
||||
fn(field.String())
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
fn(val.Field(i).String())
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,14 @@ func TestWalk(t *testing.T) {
|
||||
}{"Chris"},
|
||||
[]string{"Chris"},
|
||||
},
|
||||
{
|
||||
"struct with two string fields",
|
||||
struct {
|
||||
Name string
|
||||
City string
|
||||
}{"Chris", "London"},
|
||||
[]string{"Chris", "London"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range cases {
|
||||
|
Loading…
Reference in New Issue
Block a user