gpath

package module
v0.0.0-...-3e6e957 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2017 License: MIT Imports: 7 Imported by: 3

README

gpath Go Documentation Travis Go Report Card codecov

gpath is a Go package to access a field by a path using reflect pacakge.

A path is represented by a Go's expression such as A.B.C[0]. You can use selector and index expressions into a path.

See usage and example in GoDoc.

NOTE: This package is experimental and may make backward-incompatible changes.

Install

Use go get:

$ go get github.com/tenntenn/gpath

Usage

All usage are described in GoDoc.

mercari/go-httpdoc is a good example for gpath.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func At

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.

Example
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

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL