A Go library to recursively visit data structures using reflection.
import "github.com/keilerkonzept/visit"
go get -u "github.com/keilerkonzept/visit"
import (
"github.com/keilerkonzept/visit"
"fmt"
"reflect"
)
type myStruct struct {
String string
Map map[string]myStruct
Ptr *myStruct
}
func main() {
obj := &myStruct{
String: "hello",
Map: map[string]myStruct{
"world": myStruct{String: "!"},
},
}
obj.Ptr = obj
var strings []string
visit.Values(obj, func(value visit.ValueWithParent) (visit.Action, error) {
if value.Kind() == reflect.String {
strings = append(strings, value.String())
}
return visit.Continue, nil
})
fmt.Println(strings)
// Output:
// [hello world !]
}