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)) {
|
func Walk(x interface{}, fn func(string)) {
|
||||||
val := reflect.ValueOf(x)
|
val := reflect.ValueOf(x)
|
||||||
field := val.Field(0)
|
for i := 0; i < val.NumField(); i++ {
|
||||||
fn(field.String())
|
fn(val.Field(i).String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,14 @@ func TestWalk(t *testing.T) {
|
|||||||
}{"Chris"},
|
}{"Chris"},
|
||||||
[]string{"Chris"},
|
[]string{"Chris"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"struct with two string fields",
|
||||||
|
struct {
|
||||||
|
Name string
|
||||||
|
City string
|
||||||
|
}{"Chris", "London"},
|
||||||
|
[]string{"Chris", "London"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range cases {
|
for _, test := range cases {
|
||||||
|
Loading…
Reference in New Issue
Block a user