Skip to content

Latest commit

 

History

History
53 lines (42 loc) · 1.04 KB

README.md

File metadata and controls

53 lines (42 loc) · 1.04 KB

visit

Go Report Card

A Go library to recursively visit data structures using reflection.

import "github.com/keilerkonzept/visit"

Get it

go get -u "github.com/keilerkonzept/visit"

Use it

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 !]
}