Skip to content
Search Go's objects and ast nodes by queries
Go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis.yml
LICENSE
README.md
array.go
assign_node.go
basic_size.go
bool.go
chan.go
complex.go
float.go
func.go
goq.go
goq_test.go
int.go
interface.go
named.go
node.go
package.go
query.go
seq.go
set.go
signature.go
slice.go
string.go
struct.go
tupple.go
type.go
type_name.go
var.go

README.md

goq Go Documentation Travis

goq is a Go package to access nodes of Go's AST by queries.

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/goq

Usage

All usage are described in GoDoc.

const src = `package main
func main() {
	n := 10
	println(n)
}`

func run() error {
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "main.go", src, 0)
	if err != nil {
		return err
	}

	config := &types.Config{
		Importer: importer.Default(),
	}

	info := &types.Info{
		Defs: map[*ast.Ident]types.Object{},
		Uses: map[*ast.Ident]types.Object{},
	}

	files := []*ast.File{f}
	if _, err := config.Check("main", fset, files, info); err != nil {
		return err
	}

	results := goq.New(fset, info, files).Query(&Int{})
	for _, r := range results {
		fmt.Println(r.Object, "at", fset.Pos(r.Node.Pos()))
	}

	return nil
}

func main() {
	if err := run(); err != nil {
		fmt.Fprintln(os.Stderr, "Error:", err)
		os.Exit(1)
	}
}
You can’t perform that action at this time.