https://github.com/nadim147c/go-tree
📦 A simple Go package for traversing 🌲tree like deeply nested data in go
https://github.com/nadim147c/go-tree
golang json package web-scraping
Last synced: about 2 months ago
JSON representation
📦 A simple Go package for traversing 🌲tree like deeply nested data in go
- Host: GitHub
- URL: https://github.com/nadim147c/go-tree
- Owner: Nadim147c
- License: lgpl-3.0
- Created: 2025-04-18T10:19:13.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-10T08:21:13.000Z (about 1 year ago)
- Last Synced: 2025-08-01T00:31:00.928Z (11 months ago)
- Topics: golang, json, package, web-scraping
- Language: Go
- Homepage:
- Size: 42 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
go-tree
Search and traverse deeply nested data structures.
## Description
A Go package for **searching** and **traversing** deeply nested data structures
such as maps, slices, arrays, and structs. It offers flexible utilities for extracting specific
values or entire subtrees using custom filter logic.
## Installation
```bash
go get github.com/Nadim147c/go-tree/v3
```
## Features
- Depth-first traversal of nested structures
- Customizable filtering
- Supports maps, slices, arrays, structs, and primitive types
- Clear and predictable error handling
## Usage
See the [example](./example/) directory for practical examples.
## How it Works
`go-tree` provides following capabilities:
### Find
- `Find` functions (`FindString`, `FindBool`, `FindInt`, `FindUint`, `FindFloat`) scan the
tree and return the **first primitive value** of the specified type that satisfies the filter.
- Type checks are performed **before** the filter is applied, ensuring only correctly-typed values
are evaluated.
- The generic `Find` function returns the first matching **branch** (e.g., `map`, `slice`, `struct`)
without performing type filtering — useful for targeting nested structures for deeper inspection.
### Collect
- `Collect` functions collect **all primitive values** of the specified type that match the
filter. These include `CollectString`, `CollectBool`, `CollectInt`, `CollectUint`, and
`CollectFloat`.
- Type enforcement is done before applying the filter, ensuring that only relevant values are
processed.
- The generic `Collect` collects **all matching branches**, useful for aggregating nested
collections of interest, regardless of their concrete type.
### Has
- `Has` functions (`HasString`, `HasBool`, `HasInt`, `HasUInt`, `HasFloat`) traverse the
structure and return `true` if **any value of the specified type** satisfies the filter function.
- These functions **automatically perform type checking** before applying the filter, which
simplifies filter logic but also restricts it to only matching values of the expected type.
- The generic `Has` does **not enforce type constraints**, making it useful when the filter logic
needs to inspect or match across **multiple types or complex conditions**.
# Summary
- For **primitive values**, always prefer `Find` and `Traverse`.
- Use **generic `Find` and `Traverse`** when working with **nested objects**.
- Full API documentation is available through GoDoc or your IDE.
- See [example](./example/) for practical usage.
## License
This package is licensed under [GNU-LGPL-3.0](./LICENSE).