Skip to content

keilerkonzept/visit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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