https://github.com/mcuadros/go-lookup
Small library on top of reflect for make lookups to any Structs or Maps
https://github.com/mcuadros/go-lookup
Last synced: 2 months ago
JSON representation
Small library on top of reflect for make lookups to any Structs or Maps
- Host: GitHub
- URL: https://github.com/mcuadros/go-lookup
- Owner: mcuadros
- License: mit
- Created: 2015-07-08T16:47:17.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2023-06-27T15:02:33.000Z (almost 2 years ago)
- Last Synced: 2025-04-06T12:36:42.850Z (3 months ago)
- Language: Go
- Homepage: https://pkg.go.dev/github.com/mcuadros/go-lookup
- Size: 30.3 KB
- Stars: 43
- Watchers: 3
- Forks: 14
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
go-lookup [](https://github.com/mcuadros/go-lookup/actions) [](https://pkg.go.dev/github.com/mcuadros/go-lookup)
==============================Small library on top of reflect for make lookups to Structs or Maps. Using a very simple DSL you can access to any property, key or value of any value of Go.
Installation
------------The recommended way to install go-lookup
```
go get github.com/mcuadros/go-lookup
```Example
-------```go
type Cast struct {
Actor, Role string
}type Serie struct {
Cast []Cast
}series := map[string]Serie{
"A-Team": {Cast: []Cast{
{Actor: "George Peppard", Role: "Hannibal"},
{Actor: "Dwight Schultz", Role: "Murdock"},
{Actor: "Mr. T", Role: "Baracus"},
{Actor: "Dirk Benedict", Role: "Faceman"},
}},
}q := "A-Team.Cast.Role"
value, _ := LookupString(series, q)
fmt.Println(q, "->", value.Interface())
// A-Team.Cast.Role -> [Hannibal Murdock Baracus Faceman]q = "A-Team.Cast[0].Actor"
value, _ = LookupString(series, q)
fmt.Println(q, "->", value.Interface())
// A-Team.Cast[0].Actor -> George Peppard
```### Case-insensitive matching
Use the `LookupI` and `LookupStringI` functions to do a case-insensitive match on struct field names and map keys. It will first look for an exact match; if that fails, it will fall back to a more expensive linear search over fields/keys.
```go
type ExampleStruct struct {
SoftwareUpdated bool
}i := ExampleStruct{
SoftwareUpdated: true,
}value, _ := LookupStringI(i, "softwareupdated")
fmt.Println(value.Interface())
// Output: true
```License
-------MIT, see [LICENSE](LICENSE)