gpath: github.com/tenntenn/gpath Index | Examples | Files

package gpath

import "github.com/tenntenn/gpath"

Index

Examples

Package Files

eval.go gpath.go

func At Uses

func At(v interface{}, path string) (interface{}, error)

At access a field of v by a path. v must be struct or pointer of struct. A path is represented by Go's expression which can be parsed by go/parser.ParseExpr. You can use selectors and indexes in a path. Slice and arrays index allow only expressions of int. Maps key allow only expressions of string, int and float64.

Code:

type Bar struct {
    N []int
}

type Foo struct {
    Bar *Bar
}

f := &Foo{
    Bar: &Bar{
        N: []int{100},
    },
}

v, err := At(f, `Bar.N[0]`)
if err != nil {
    fmt.Println(err)
} else {
    fmt.Println(v)
}

Output:

100

Package gpath imports 7 packages (graph) and is imported by 1 packages. Updated 2017-06-04. Refresh now. Tools for package owners.