https://github.com/keilerkonzept/visit
Go library to recursively visit data structures using reflection. #golang
https://github.com/keilerkonzept/visit
go golang map recursive reflection slice struct tree visit
Last synced: 8 months ago
JSON representation
Go library to recursively visit data structures using reflection. #golang
- Host: GitHub
- URL: https://github.com/keilerkonzept/visit
- Owner: keilerkonzept
- License: mit
- Created: 2019-12-28T10:30:23.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-12-29T13:26:20.000Z (almost 6 years ago)
- Last Synced: 2025-03-08T09:03:30.911Z (8 months ago)
- Topics: go, golang, map, recursive, reflection, slice, struct, tree, visit
- Language: Go
- Homepage: http://godoc.org/github.com/keilerkonzept/visit
- Size: 15.6 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# visit
[](http://godoc.org/github.com/keilerkonzept/visit) [](https://goreportcard.com/report/github.com/keilerkonzept/visit)
A Go library to recursively visit data structures using reflection.
```go
import "github.com/keilerkonzept/visit"
```
## Get it
```sh
go get -u "github.com/keilerkonzept/visit"
```
## Use it
```go
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 !]
}
```